Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to populate array with lines from text file? (Moved from Q&A)

by repson (Chaplain)
on Dec 09, 2000 at 14:39 UTC ( [id://45876]=note: print w/replies, xml ) Need Help??


in reply to How to populate array with lines from text file? (Moved from Q&A)

Perl has an easy syntax for this, just use:
open (FH, "< $file") or die "Can't open $file for read: $!"; my @lines = <FH>; close FH or die "Cannot close $file: $!";
Since <FH> is in array context ie. the left hand side of the assignment expects a list of items, it will fill the array with all the lines in the file.

A few other points, your open shouldn't have angle brackets, @lines and $file should be declared with my() (I don't know why you aren't getting a compile time error from strict) and $line should be $_ (if you want to continue with the while loop).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://45876]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found