Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Reading multiple files into array

by perlNinny (Beadle)
on May 10, 2007 at 17:05 UTC ( #614695=perlquestion: print w/replies, xml ) Need Help??

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

This only seems to keep the last file read:
use FileHandle; my @list; my $file; my @d = ("ds","DD","df"); foreach $type (@d){ $file = "list_".$type.".txt"; open (fh, "<$file"); @list = <fh>; close fh; } foreach $output (@list){ chomp $output; print $output."\n"; }
So how do I put ALL the lines from ALL the files into @list?

Replies are listed 'Best First'.
Re: Reading multiple files into array
by philcrow (Priest) on May 10, 2007 at 17:10 UTC
    When you say '@list = ...' the old value will be discarded and the new value will replace it. To get what you want, try push:
    push @list, <fh>;
    Phil
    The Gantry Web Framework Book is now available.
      Quote the Homer: D'OH That did it.
Re: Reading multiple files into array
by leocharre (Priest) on May 11, 2007 at 13:30 UTC
    unix 'cat' could be of interest to you. Perl versions are available on cpan.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2022-08-14 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?