Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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 ( #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).


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (16)
As of 2015-07-02 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (45 votes), past polls