in reply to Array question

I thought I'd post this solution because it has the two different sorts needed.

Chris

#!/usr/bin/perl use strict; use warnings; use Socket; # program uses the inet_aton and inet_ntoa functions my %month; @month{ qw/ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = '01'.. +'12'; my %data; while (<DATA>) { my ($date, $ip, $count1, $count2) = split /,/; $data{$date}{ inet_aton($ip) }{count1} += $count1; $data{$date}{ inet_aton($ip) }{count2} += $count2; } for my $date (sort by_date keys %data) { for my $ip (sort keys %{ $data{$date} }) { my $c1 = $data{$date}{$ip}{count1}; my $c2 = $data{$date}{$ip}{count2}; print join(",", $date, inet_ntoa($ip), $c1, $c2), "\n"; } } sub by_date { my ($d_a, $m_a, $y_a) = split ' ', $a; my ($d_b, $m_b, $y_b) = split ' ', $b; my $A = "$y_a$month{$m_a}$d_a"; my $B = "$y_b$month{$m_b}$d_b"; $A cmp $B; } __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 03 Aug 2007,221.6.110.9,1,2 03 Aug 2007,221.6.110.9,10,10 ***prints C:\perlp>perl t7.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 03 Aug 2007,221.6.110.9,11,12

Replies are listed 'Best First'.
Re^2: Array question
by Anonymous Monk on Sep 06, 2007 at 04:56 UTC
    Thanks to one and all. My apology for delayed response. I have taken the logic of GrandFather and tweaked it to my requirment it WORKED. I was caught up with that work. Special thanks to GrandFather. I cannot do it from the database as there is a constraint in the logic built. Thanks, Dinakar