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

Re: Appending arrays into the rows of a 2 dimension array

by poj (Abbot)
on Jun 03, 2014 at 20:45 UTC ( [id://1088515]=note: print w/replies, xml ) Need Help??


in reply to Appending arrays into the rows of a 2 dimension array

#!perl use strict; my %data=(); my @data1 = ( '8 14.701072 3.011583 2.943297 2.940214 2.955738 2.968009', '16 30.561410 6.249463 6.118992 6.124449 6.112282 6.133605', '32 60.983738 12.254996 12.263543 12.277047 12.291713 12.196748', '64 116.472912 24.024475 23.347609 23.463421 23.294582 23.336542'); my @data2 = ( '8 14.788687 3.083707 2.958022 2.957737 2.982671 2.990788', '16 30.368369 6.384345 6.079698 6.073674 6.108037 6.134851', '32 60.373260 12.492582 12.137764 12.164201 12.074652 12.107912', '64 117.676048 24.010324 23.535210 23.562157 23.596123 23.675554'); for (@data1,@data2){ my ($sz,$sum,@f) = split /\s+/; push @{$data{$sz}},$_ for @f; } for my $sz (sort {$a <=> $b} keys %data){ print join ' ',@{$data{$sz}},"\n"; }
poj

Replies are listed 'Best First'.
Re^2: Appending arrays into the rows of a 2 dimension array
by PerlSufi (Friar) on Jun 03, 2014 at 21:09 UTC
    Taking poj's nice code to use filehandle:
    use strict; use warnings; use autodie; my %data=(); my $file_one = 'test.txt'; my $file_two = 'test2.txt'; open ( my $fh1, '<', $file_one); open ( my $fh2, '<', $file_two); my @data1 = <$fh1>; my @data2 = <$fh2>; for (@data1,@data2){ my ($sz,$sum,@f) = split /\s+/; push @{$data{$sz}},$_ for @f; } for my $sz (sort {$a <=> $b} keys %data){ print join ' ',@{$data{$sz}},"\n"; }
    UPDATE: if the data files actually contain new lines, chomp($_) in the first for loop
      Hi PerlSufi, your code is very fine for the example, but it does not scale up very well for more than two files (and the OP mentioned that there are multiple data files).

      I would probably avoid storing each file into individual arrays, and try to process each file sequentially, and change the code to something like this (incomplete and obviously untested):

      use strict; use warnings; my %data=(); for my $file (qw /file_1.txt file_2.txt, file_3.txt ... file_n.txt/) { open my $FH, "<", $file or die "could not open $file $!"; while (<$FH>) { chomp; my ($sz,$sum,@f) = split /\s+/; push @{$data{$sz}},$_ for @f; } close $FH; } # ...
      Or possibly, if the files are passed as arguments to the script:
      use strict; use warnings; my %data=(); for my $file (@ARGV) { open my $FH, "<", $file or die "could not open $file $!"; while (<$FH>) { # ... } # ... } # ...
      or even (still assuming the files are passed as arguments):
      use strict; use warnings; use autodie; my %data=(); while (<>) { chomp; # ... } #...
      This latest solution might be used even if the argument passed to the script is not a list of files, but, say, the directory where they are stored:
      use strict; use warnings; use autodie; my $stat_dir = shift; my %data = (); { local @ARGV = glob ("$stat_dir/*.*"); while (<>) { chomp; # ... } # ... }
      Admittedly, the latest solutions look less robust and one might want to avoid them for production code. But are they really less robust? Hmm, if glob returns a list of files, then you basically know the files are there, the only thing that is lacking might be checking read privileges, no big deal.

        Awesome Laurent_R. I thought about the @ARGV solution but the other ones are great, too- thanks :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found