在 Perl 中直接索引到返回的数组 [英] Indexing directly into returned array in Perl
问题描述
关于 PHP 的这个问题已经在 here和这里,我对 Perl 有同样的问题.给定一个返回列表的函数,是否有任何方法(或最好的方法)可以在不使用临时变量的情况下立即对其进行索引?
This question has been asked about PHP both here and here, and I have the same question for Perl. Given a function that returns a list, is there any way (or what is the best way) to immediately index into it without using a temporary variable?
例如:
my $comma_separated = "a,b,c";
my $a = split (/,/, $comma_separated)[0]; #not valid syntax
我明白为什么第二行中的语法无效,所以我想知道是否有一种方法可以获得相同的效果,而无需先将返回值分配给列表并从中建立索引.
I see why the syntax in the second line is invalid, so I'm wondering if there's a way to get the same effect without first assigning the return value to a list and indexing from that.
推荐答案
只需使用括号来定义您的列表,然后将其索引以提取您想要的元素:
Just use parentheses to define your list and then index it to pull your desired element(s):
my $a = (split /,/, $comma_separated)[0];
这篇关于在 Perl 中直接索引到返回的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!