while (<FILE>) {
my ($name, $addr, undef, undef) = split /:/;
...
}
这里的undef表示对split产生的list中的第三项和第四项不感兴趣,也就是忽略这两项的意思。
list 的一部分——slice
my $mtime = (stat $some_file)[9]; # 表示stat产生的list的第9项
my $count = (split /:/)[5];
my ($num, $count) = (split /:/)[1,5];
my ($first, $last) = (sort @names)[0,-1];
my @numbers = (@names)[9,0,2,1]; # 顺序可以随意
上面的例子可以缩写为:
my @numbers = @names[9,0,2,1]; # 顺序可以随意
hash 的 slice
补充说明一下:slice 始终都还是一个 list ,一般由原来的list处理后产生。
hash也有slice。
比如一般的写法:
my @three_scores = ($score{"barney"}, $score{"fred"}, $score{"dino"});
采用slice写法:
my @three_scores = @score{qw/ barney fred dino /});
其他的例子:
@score{@players} = @bowling_scores;
print "@score{ @players }";