Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

store multiple files in multidimensional array

by bazi (Novice)
on Mar 18, 2012 at 20:43 UTC ( [id://960321]=perlquestion: print w/replies, xml ) Need Help??

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

Hey, I have a dir of 10 files (same number of lines, words in lines but different data) and I would like to go thru that dir in a loop and create multidimensional array of all files. Any way to do it? With one file I just do I could walk thru all files and add line by line as an element moving thru the 2d array but I am looking for more elegant solution, something like below though it is missing this important piece of code:
my @combinedData; my @currentFile; my @filesList = <*>; foreach my $file (@filesList) { open(F, "file.txt") or die("Unable to open file"); @currentFile = <F>; # HOW TO COMBINE IT INTO @combinedData ? # if I do it thru a reference the content # will change in the next loop when I modify @currentFile # again ... }
Any suggestins are very mcuh appreciated. Thanks Bazi

Replies are listed 'Best First'.
Re: store multiple files in multidimensional array
by toolic (Bishop) on Mar 18, 2012 at 21:19 UTC
    This stuffs all file contents into a hash-of-arrays:
    use warnings; use strict; use File::Slurp; my %data; for (glob '*') { push @{ $data{$_} }, read_file($_); }

    See also:

    Same with array-of-arrays:

    my @data; for (glob '*') { push @data, [ read_file($_) ]; }
Re: store multiple files in multidimensional array
by repellent (Priest) on Mar 19, 2012 at 02:06 UTC
    If you're globbing as in @filesList = <*> or @filesList = glob "*", consider what the order of the files would be in the array. You may want it sorted:
    bash-3.2$ cat d1 abc cde edf bash-3.2$ cat d2 rst tuv uvw bash-3.2$ cat d3 ghi hij jkl bash-3.2$ cat t4.pl my %data; while (<>) { chomp; push @{ $data{$ARGV} }, $_; } my @sorted_data = map $data{$_}, sort keys %data; use Data::Dumper; warn Dumper \@sorted_data; bash-3.2$ t4.pl * $VAR1 = [ [ 'abc', 'cde', 'edf' ], [ 'rst', 'tuv', 'uvw' ], [ 'ghi', 'hij', 'jkl' ] ];
Re: store multiple files in multidimensional array
by Anonymous Monk on Mar 19, 2012 at 09:05 UTC

    if I do it thru a reference the content will change in the next loop when I modify @currentFile again

    Don't define @currentfile in that scope then.

    my @combinedData; my @filesList = <*>; foreach my $file (@filesList) { my @currentFile; open(F, "file.txt") or die("Unable to open file"); @currentFile = <F>; # HOW TO COMBINE IT INTO @combinedData ? push @combinedData, \@currentFile; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found