Say a Perl subroutine returns an array:
sub arrayoutput
{
...some code...
return @somearray;
}
I want to access only a specific array element from this, say the first. So I could do:
@temparray=arrayoutput(argument);
and then refer to $temparray[0]
.
But this sort of short reference doesn't work: $arrayoutput(some argument)[0]
.
I am used to Python and new to Perl, so I'm still looking for some short, intuitive, python-like way (a=arrayoutput(some argument)[0]
) to get this value. My Perl programs are getting very long and using temporary arrays like that seems ugly. Is there a way in Perl to do this?
use warnings;
use strict;
sub foo {
return 'a' .. 'z'
}
my $y = (foo())[3];
print "$y\n";
__END__
d
Alternately, you do not need an intermediate variable:
use warnings;
use strict;
sub foo {
return 'a' .. 'z'
}
print( (foo())[7], "\n" );
if ( (foo())[7] eq 'h') {
print "I got an h\n";
}
__END__
h
I got an h