in reply to Array question
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
Update: Renamed variables from $a, $b to $i, $j as adviced by GrandFather.$ 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Array question
by GrandFather (Saint) on Sep 05, 2007 at 10:53 UTC | |
by narainhere (Monk) on Sep 05, 2007 at 10:59 UTC | |
by GrandFather (Saint) on Sep 05, 2007 at 11:04 UTC |