David Shere wrote:
Hello.
Hello,
I'm not a new perl programmer, but I feel like one today. I
want to pull the last octet off of an IP address and print it to
standard output. I have this so far:
@octets = split(/\./, $ipAddress);
print pop(@octets);
Which works great. I have no other use for @octets, so I should be able
to just pass the results of split() right to pop():
print pop(split(/\./, $ipAddress));
However, I get the error message
Type of arg 1 to pop must be array (not split) at ./oct.pl line
8, near "))"
I realize I need to make sure the results of split() are an array before
they're passed to pop(). Fine. However,
print pop(@{split(/\./, $ipAddress)});
prints nothing. split() *does* return an array, right?
No, funtions and subroutines return lists.
perldoc -q "What is the difference between a list and an array"
Why can't pop take it?
You could copy the list to an array:
$ perl -le'
my $ipAddress = "23.34.45.56";
print pop @{[ split /\./, $ipAddress ]};
'
56
Or, as others have suggested, just access the last list element:
$ perl -le'
my $ipAddress = "23.34.45.56";
print +( split /\./, $ipAddress )[ -1 ];
'
56
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/