我正在尝试拆分一个字符串并从返回的数组中弹出一个值。我怎样才能在一行中做到这一点?
my $string = 'test,string';
# Split and pop in one line
my $val = pop [split ',', $string];
print $val;
结果应该是
'string'
但相反,我得到了这个:Type of arg 1 to pop must be array (not anonymous list ([]))
这是我的 Perl 版本信息,以防万一:
This is perl, v5.8.8 built for x86_64-linux-thread-multi
最佳答案
push
, pop
, shift
, 和 unshift
用于操作数组。听起来您只想从 split
返回的列表中获取最后一个值。 ,您可以通过列表切片获得:
my $val = (split /,/, $foo)[-1];
关于Perl 在一行中拆分和弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32913529/