in reply to Re: Array question
in thread Array question

Actually this is a table reterived from the database and i tried this
my @new_duplicateip_results; foreach my $first (@$select_result) { foreach my $second (@$select_result) { if ((@$first->[0] eq @$second->[0]) && (@$first->[1] eq @$seco +nd->[1]) && (@$first->[$#{$first}] eq $self->product_id) && (@$second +->[$#{$second}] eq "WS-".$self->product_id)) { my $ip_string = "@$first->[0],@$first->[1],"; foreach (2..$#{$first}) { $ip_string .= @$first->[$_]+@$second->[$_].","; } my @split_array = split(/,/,$ip_string) ; push @new_duplicateip_results,\@split_array; } } } foreach (@new_duplicateip_results) { print STDERR join "#",@$_; print STDERR "\n"; }
However i need the values that are not matching the creiria. If i include else then multiple rows are fetched. Thanks, Dinakar

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

    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
      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
      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
Re^3: Array question
by eric256 (Parson) on Sep 05, 2007 at 21:34 UTC

    If this is a table why don't you let the database do the work?

    SELECT date, ip, sum(a), sum(b) FROM table GROUP BY date, ip

    ___________
    Eric Hodges