Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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 about the Monastery: (7)
As of 2014-07-29 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (211 votes), past polls