我怎么可以很方便地通过一个Perl子程序返回的元素? [英] How can I selectively access elements returned by a Perl subroutine?
问题描述
说一个Perl函数返回一个数组:
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);
然后参照 $ temparray [0]
。
但是,这种短期参考不起作用: $ arrayoutput(参数部分)[0]
But this sort of short reference doesn't work: $arrayoutput(some argument)[0]
.
我用Python和新的Perl,所以我仍然在寻找一些短期的,直观的,蟒蛇般的方式( A = arrayoutput(参数部分)[0]
)来得到这个值。我的Perl程序越来越很长,使用临时数组一样,似乎难看。有没有在Perl的方式做到这一点?
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
更新:另一个code为例,以解决您的评论。你并不需要一个中间变量:
UPDATE: Another code example to address your comment. 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
这篇关于我怎么可以很方便地通过一个Perl子程序返回的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!