Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Perl newbie question


  • Please log in to reply

#1
idrank

idrank

    New Member

  • Member
  • Pip
  • 1 posts
Hi,
I am having a problem reading input from a file into an array. My simple code is:

#***********************************************************
#!usr/bin/perl

open DATA, "<test.dat";


chomp(@strings = <DATA>); #read the lines, not the newlines

print "@strings";

close DATA;

#***********************************************************

where the datafile 'test.dat' contains something simple, something like:

a 5 4 3
b 4 5 8
d 1 4 2
c 0 0 8

All I want to do is read each line into an element of an array, @strings, without the newline characters. When I run the above code, however, it will only print out 'c 0 0 8'. How come it doesn't print out all of the elements? This works fine if the input comes from <STDIN>. What am I missing here?

Thanks
  • 0

Advertisements


#2
BuRnEr

BuRnEr

    Member

  • Member
  • PipPip
  • 48 posts
#!usr/bin/perl

open DATA, "<test.dat";
@strings = <DATA>; #correction
close DATA;
print @strings; #correction

#close DATA;

and run it again ....

take Care
BuRnEr
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP