in reply to Re^4: Array question
in thread Array question

Sure:

for my $datum (map {[++$sortKey, "$_->[0],$_->[1]", @$_]} @data) { if (exists $sums{$datum->[1]}) { $sums{$datum->[1]}[$_] += $datum->[$_] for 4 .. $#$datum; } else { $sums{$datum->[1]} = $datum; } } my @result = map {[@{$_}[2 .. $#$_]]} sort {$a->[0] cmp $b->[0]} value +s %sums;

Take note of the two places where $# is used to get the last array index. Note that this technique deals with differing column counts on a row by row basis. Unlikely to be an issue in your current case I'd guess, but neat anyway. ;)

The code does assume that columns are left aligned and thus, any missing columns are assumed to be missing from the high index end of the array (in my mind index 0 is left of index 1).


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^6: Array question
by udinakar (Novice) on Sep 06, 2007 at 13:18 UTC
    Thanks a lot GrandFather, It worked well for me :) You have made my day easier. thanks, ~Din