For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2007 > confusion with splitting columns using [-n, -n] (e.g; my ( $country, $bytes ) = (









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author confusion with splitting columns using [-n, -n] (e.g; my ( $country, $bytes ) = (
Michael Alipio

2007-01-30, 3:58 am

Hi,

I have a file that look like this:

1668 | 172.194.177.182 | US 12679172
10396 | 64.237.148.157 | PR 12679172
9318 | 211.187.212.242 | KR 1279172
22291 | 66.215.254.186 | US 1269172
22291 | 24.176.212.76 | US 1679172
30225 | 66.147.146.214 | US 2679172
17676 | 221.34.8.92 | JP 1267173
17858 | 125.180.111.187 | KR 12679172
6395 | 67.96.150.40 | US 12679172
17858 | 125.180..193.124 | KR 12679175
3462 | 218.168.176.39 | TW 12679472
9919 | 218.211.204.195 | TW 12666172
9318 | 222.235.22.225 | KR 12672272
9318 | 222.237.14.160 | KR 12679142



Six columns including two colums with pipe symbols.
The goal is to add up the values in the last column that belongs to the same country. That last column are the bytes received by a particular country.So I have to add all bytes received by US, KR, etc.

Someone has givenme this code:

open WHOISWITHBYTES, '<', "whois.bytes" or die $!;

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

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

It is working perfectly but now, I needto document this code. Can anyone help me out on understanding this code.

I'm particularly with the line:
"my ($country, $bytes) = (split) [-2, -1];

What does this tells? What does -2 and -1 tells?All I know is that split will output a list containing two values that will be assigned to $country and $bytes for every line of that whois.bytes file. But I'm not sure what those -2,-1 means and how it was able to extract column 5 and 6. I tried looking at perldoc -f split but cannot seem to find the explanation. Are those the LIMIT thing?


Thanks!






________________________________________
________________________________________
____
Any questions? Get answers on any topic at www.Answers.yahoo.com. Try it now.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com