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

I apologize for leaping to the worst interpretation of your post! I hope the code below helps. It will muck up the date ordering, but I've run out of time tonight to clean that up.

use strict; use warnings; use Data::Dump::Streamer; my @data = ( ['01 Jul 2007','221.6.19.196',9,19], ['01 Jul 2007','221.6.19.197',1,3], ['01 Jul 2007','221.6.19.196',12,12], ['01 Jul 2007','221.6.19.197',2,2], ['02 Jul 2007','202.119.104.22',1,1], ['02 Jul 2007','221.6.19.196',20,45], ['03 Jul 2007','202.119.104.12',1,11], ['03 Jul 2007','202.119.110.236',1,2], ['03 Jul 2007','210.29.132.9',1,2], ['03 Jul 2007','210.29.141.188',1,2], ['03 Jul 2007','221.6.19.195',3,7], ['03 Jul 2007','221.6.19.196',4,7], ['03 Jul 2007','222.192.2.213',1,0], ['03 Jul 2007','202.119.108.42',3,3], ); my %sums; $sums{$_->[0]}{a} += $_->[1], $sums{$_->[0]}{b} += $_->[2] for map {["$_->[0],$_->[1]", @{$_}[2,3]]} @data; my @result = map {[@{$_}[1, 2, 3, 4]]} sort {$a->[0] cmp $b->[0]} map {[$_, split (',', $_), @{$sums{$_}}{'a', 'b'}]} keys %sums; Dump (\@result);

Prints:

$ARRAY1 = [ [ '01 Jul 2007', '221.6.19.196', 21, 31 ], [ '01 Jul 2007', '221.6.19.197', 3, 5 ], [ '02 Jul 2007', '202.119.104.22', ( 1 ) x 2 ], [ '02 Jul 2007', '221.6.19.196', 20, 45 ], [ '03 Jul 2007', '202.119.104.12', 1, 11 ], [ '03 Jul 2007', '202.119.108.42', ( 3 ) x 2 ], [ '03 Jul 2007', '202.119.110.236', 1, 2 ], [ '03 Jul 2007', '210.29.132.9', 1, 2 ], [ '03 Jul 2007', '210.29.141.188', 1, 2 ], [ '03 Jul 2007', '221.6.19.195', 3, 7 ], [ '03 Jul 2007', '221.6.19.196', 4, 7 ], [ '03 Jul 2007', '222.192.2.213', 1, 0 ] ];

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^4: Array question
by Anonymous Monk on Sep 05, 2007 at 13:02 UTC
    Thanks a bunch GrandFather, I am working on your solution to fit my requirment. Sorry in sending the delayed response. Will let you know once done. Cheers, Dinakar

      It occurred to me on the walk to work this morning that the sort problem can be fixed by introducing a sort key:

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

      Output is as shown previously - the dates are all in the same month.


      DWIM is Perl's answer to Gödel
Re^4: Array question
by udinakar (Novice) on Sep 06, 2007 at 08:58 UTC
    Thanks a lot GrandFather, It worked well for me :) You have made my day easier. thanks, ~Din

      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