Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

reading multiple files into an array

by thatguy (Parson)
on Aug 27, 2001 at 20:09 UTC ( [id://108161]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Re: Re: Unix Perl and Html
in thread Unix Perl and Html

something like this will read in all the files listed in @files. It also cuts down on repetitive code and allows scalability.

my @files= qw| /path/to/file1 /path/to/file2 /path/to/file3 |; my @allfiles; for my $filename(@files){ open(FILE," $filename") || die "Cannot open $filename: $!\n"; while(<FILE>) { push @allfiles, $_; } close(FILE); }

-p

Replies are listed 'Best First'.
Re: reading multiple files into an array
by dragonchild (Archbishop) on Aug 27, 2001 at 20:46 UTC
    Better would be:
    my @files= qw| /path/to/file1 /path/to/file2 /path/to/file3 |; my @allfiles; for my $filename(@files){ open FILE, $filename || die "Cannot open $filename for reading: $!\n"; push @allfiles, $_ while (<FILE>); close FILE; } # Or (and even more Perlish) ... my @files= qw| /path/to/file1 /path/to/file2 /path/to/file3 |; my @allfiles; for my $filename(@files){ open FILE, $filename || die "Cannot open $filename for reading: $!\n"; push @allfiles, <FILE>; close FILE; }

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

Log In?
Username:
Password:

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

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

    No recent polls found