Re: confusion with splitting columns using [-n, -n] (e.g; my ( $country, $bytes ) = ( split )[ -2, -1 ])
- From: Jenda@xxxxxxxxxxx ("Jenda Krynicky")
- Date: Tue, 30 Jan 2007 14:39:21 +0100
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?
No these are not the LIMIT. These are array indices. You could write
the code like this:
my @items = split;
my ($country, $bytes) = @items[-2,-1];
The
my @items = split;
line splits the contents of $_ on whitespace and stores the results
in the @items array, the
my ($country, $bytes) = @items[-2,-1];
copies the last-but-one element of @items to $country and the last
one to $bytes.
You can use the
(function_call( parameters))[indices]
shortcut with any function that returns several values (eg.
localtime()), but you have to make sure you enclose the function call
in braces.
HTH, Jenda
===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
.
- Prev by Date: execution of script in one shell
- Next by Date: SQL Server Connectivity
- Previous by thread: execution of script in one shell
- Next by thread: SQL Server Connectivity
- Index(es):
Relevant Pages
|