Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Making an array out of each line in a file

by exfacior (Initiate)
on Nov 11, 2004 at 21:52 UTC ( [id://407192]=perlquestion: print w/replies, xml ) Need Help??

exfacior has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys im new here! Your articles and questions have helped me alot. Just a quick one here.
I read from Ovid how to read each line from a file into a variable ala

open (FILE,"filepath") || die "Can't open file"; chomp ( my ( $var1 , $var2 , $var3 ) = <FILE> ); close (FILE)
but was wondering if it were possible to assign all the lines to an array just as easily.

in my case, there are 11 lines, and each line contains one int.

Replies are listed 'Best First'.
Re: Making an array out of each line in a file
by TedYoung (Deacon) on Nov 11, 2004 at 21:54 UTC

    Sure,

    my @lines = <FILE>;

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

      chomp( my @lines = <FILE> );

Re: Making an array out of each line in a file
by atcroft (Abbot) on Nov 11, 2004 at 21:56 UTC
    open(FILE, "filepath") || die "Can't open file"; @array = <FILE>; close(FILE);

    So that line 1 in your file will be in $array[0], line 2 in $array[1], etc.

    Hope that helps.

Re: Making an array out of each line in a file
by pg (Canon) on Nov 11, 2004 at 22:13 UTC

    One way is to use Tie::File.

    test.pl: use Tie::File; use Data::Dumper; my $self = tie @array, "Tie::File", "test.pl"; print Dumper(\@array);
Re: Making an array out of each line in a file
by FoxtrotUniform (Prior) on Nov 11, 2004 at 21:58 UTC
    open FILE, '<', $filename or die "Can't open $filename: $!\n"; my @lines = <FILE>; close FILE or die "Can't close $filename: $!\n";

    will fill @lines. See also the $/ variable.

    --
    Yours in pedantry,
    F o x t r o t U n i f o r m

    "Anything you put in comments is not tested and easily goes out of date." -- tye

      This caught my eye.

      Sorry for being pedant but we are opening files here, so - unless you mistype your file handle, - you can always close a filehandle. Or am I wrong?
      So there is no benefit of dieing on close. If we do not need to reset $., we need not even close it.

      Cheers,
      PerlingTheUK
        Sorry for being pedant but we are opening files here, so - unless you mistype your file handle, - you can always close a filehandle. Or am I wrong?

        No need to apologize for being pedantic :-). I can't think of a situation where close would fail on a plain old file on a local filesystem; for that, it's just habit. But networked filesystems, sockets, pipes, and so on can mess up close; besides, Always Check System Call Return Values is a good habit to get into.

        --
        Yours in pedantry,
        F o x t r o t U n i f o r m

        "Anything you put in comments is not tested and easily goes out of date." -- tye

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-19 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found