Michael Alipio wrote:
> Hi,

Hello,

> I have a long log file that looks like this:
> 
> 7491    | 210.23.185.123   | PH    202597706
> 7491    | 210.23.169.91    | PH    202594221
> 7303    | 201.252.130.245  | AR    201955854
> 9318    | 210.205.6.225    | KR    201892149
> 9930    | 210.19.229.57    | MY    201418551
> 9600    | 210.251.253.180  | JP    201362230
> 9929    | 210.82.176.84    | CN    201069109
> 7491    | 210.23.182.102   | PH    201006342
> 9600    | 210.251.253.180  | JP    201322301
> 4134    | 58.215.76.36     | CN    170125144
> 4844    | 210.23.5.177     | SG    170067928
> 
> It contains 4 columns, 1st and 2nd as well as 2nd and 3rd were
> separated by a pipe,
> My goal is to add up the last column belonging to the same
> Country (column 3) display it in descending order, such that I
> may know what country received the biggest amount of bytes. The
> countries included in the log file are not a fixed list.
> Sometimes it contains PH, JP, KR sometimes, it contains PH and
> JP only, and sometimes it contains even more countries CN, JP,
> DE, US, PH, JP, KR.
> 
> It might look like this:
> 
> 
> Country         Total Bytes
> 
> PH                 4000000000
> KR                3000000000
> JP                  2000000000
> CN                1000000000
> ...
> ...
> 
> And I'm thinking, perhaps I should get a unique list of all
> countries included in this logfile and only then I can aggregate
> their Bytes by using regexp.
> 
> Can you give me some hints on how I can accomplish this goal..

You probably want something like this:

my %data;
while ( <LOGFILE> ) {
    my ( $country, $bytes ) = ( split )[ -2, -1 ];
    $data{ $country } += $bytes;
    }

print "Country         Total Bytes\n";
for my $country ( sort { $data{ $b } <=> $data{ $a } } keys %data ) {
    print "$country              $data{ $country }\n";
    }




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to