in reply to Re^2: array of arrays
in thread array of arrays
my $Grand_Total = 0; for $i ( 0 .. $#AoA ) { $row = $AoA[$i]; for $j ( 0 .. $#{$row} ) { $Total_Balance[$i] += "$row->[$j]"; } print "the total is: ($Total_Balance[$i])<hr>\n"; $Grand_Total += $Total_Balance[$i]; } print "Grand Total is: ($Grand_Total)<hr>\n" _
But wouldn't you prefer to write:
use strict; use warnings; use List::Util qw(sum); my @AoA; while (<DATA>) { my ($index, $value) = split /\|/; push @{ $AoA[$index-1] }, $value; } my @Total_Balance = map {sum( @$_ )} @AoA; print "The total is: ($_)\n" foreach @Total_Balance; print "Grand total is: (", sum( @Total_Balance ), ")\n"; __DATA__ 1|10 1|20 1|30 1|40 1|50 2|15 2|25 2|35 3|1 3|2 3|3 3|4
This design is neither fast nor small. Its merit is that each pass through the data can only do one thing. You can understand, validate, or modify any section without concern about side effects.
|
|---|