in reply to Array question

We like to see that you have made a little effort, partly so that we know you have actually thought about the problem and are not just hoping someone will save you having to do any work, and partly to gage your level of understanding of Perl and programming.

To get you started you should take a look at perlretut and perlre to gain a little understanding of pattern matching using regular expressions.

You will also find it helpful to go back to basics by looking at hashes and their application for managing data accessed by unique keys (see perldata).


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Array question
by udinakar (Novice) on Sep 05, 2007 at 11:03 UTC
    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

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

      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