in reply to Array question

Something like:
use strict; use warnings; my %hash = (); for (<DATA>) { chomp; my ($day, $ip, $i, $j) = split(q{,}, $_); $hash{$day}{$ip}{i} += $i; $hash{$day}{$ip}{j} += $j; } foreach my $day (sort keys %hash) { foreach my $ip (sort keys %{$hash{$day}}) { print qq{$day,$ip,} . $hash{$day}{$ip}{i} . q{,}. $hash{$day}{$ip}{j}; } } __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
$ perl -l 637112.pl 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,1 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,3 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
Update: Renamed variables from $a, $b to $i, $j as adviced by GrandFather.
--
Andreas

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

    Please avoid using $a and $b as general purpose variables. They are "magic" variables used by sort.


    DWIM is Perl's answer to Gödel
      Any good pointers to read about this magic variables.Really wanna know grainy details about them

      The world is so big for any individual to conquer

        Read the sort documentation (hint: follow the link).


        DWIM is Perl's answer to Gödel