Gareth Davis
Gareth Davis

Reputation: 28059

How do I extract an element of a split array without a intermediate variable in Perl?

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

Answers (2)

rubber boots
rubber boots

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

ccheneson
ccheneson

Reputation: 49410

Try this:

my $host = (split( /\s+/, "Wibble eq wobble myhost.example.com"))[-1];

Upvotes: 6

Related Questions