忽略列表赋值中的元素的最佳方法是什么?

问题描述 投票:0回答:1

我正在使用列表分配将制表符分隔的值分配给不同的变量,如下所示:

perl -E '(my $first, my $second, my $third) = split(/\t/, qq[a\tb\tc]); say $first; say $second; say $third;'
a
b
c

要忽略某个值,我可以将其分配给虚拟变量:

perl -E '(my $first, my $dummy, my $third) = split(/\t/, qq[a\tb\tc]); say $first; say $third;'
a
c

我不喜欢有未使用的变量。还有其他方法吗?

perl
1个回答
1
投票

您可以使用undef

use warnings;
use strict;
use feature 'say';

(my $first, undef, my $third) = split(/\t/, qq[a\tb\tc]);
say $first; 
say $third;

输出:

a
c
© www.soinside.com 2019 - 2024. All rights reserved.