Reputation: 28059
Given the string Wibble eq wobble myhost.example.com
I would like to be able to extract the last element.
My current effort is:
my @parts = split( /\s+/, "Wibble eq wobble myhost.example.com");
my $host = $parts[-1];
print "$host\n";
How do I do this with out the intermediate @parts
array?
Upvotes: 2
Views: 260
Reputation: 15184
Alternatively (to the answer already given), try that:
my $host = pop [split /\s+/, "Wibble eq wobble myhost.example.com"];
or, if you dont like split:
my $host = pop [qw "Wibble eq wobble myhost.example.com"];
or, more perlish:
my $host = (qw "Wibble eq wobble myhost.example.com")[-1];
rbo
Upvotes: 1
Reputation: 49410
Try this:
my $host = (split( /\s+/, "Wibble eq wobble myhost.example.com"))[-1];
Upvotes: 6