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

Thanks a lot GrandFather, It worked well for me :) You have made my day easier. thanks, ~Din

Replies are listed 'Best First'.
Re^5: Array question
by GrandFather (Saint) on Sep 06, 2007 at 11:28 UTC

    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
      Thanks a lot GrandFather, It worked well for me :) You have made my day easier. thanks, ~Din