Re: confusion with splitting columns using [-n, -n] (e.g; my ( $country, $bytes ) = ( split )[ -2, -1 ])
- From: greenwood.andy@xxxxxxxxx (Andy Greenwood)
- Date: Tue, 30 Jan 2007 09:03:43 -0500
On 1/30/07, Michael Alipio <daem0nb0y@xxxxxxxxx> wrote:
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 given me 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 need to document this code. Can anyone help me out on understanding this code.
I'm particularly confused 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?
(split) gives you an array, and then you are referencing the last two
items. Negative array indecies mean to start that far from the end of
the list, similar to negative character offsets in substr.
Thanks!
____________________________________________________________________________________
Any questions? Get answers on any topic at www.Answers.yahoo.com. Try it now.
--
--
I'm nerdy in the extreme and whiter than sour cream
.
- Follow-Ups:
- Prev by Date: Re: execution of script in one shell
- Next by Date: How do I output an array twice via pipe instead of only once?
- Previous by thread: Re: confusion with splitting columns using [-n, -n] (e.g; my ( $country, $bytes ) = ( split )[ -2, -1 ])
- Next by thread: Re: confusion with splitting columns using [-n, -n] (e.g; my ( $country, $bytes ) = ( split )[ -2, -1 ])
- Index(es):