我有一个包含数字的数组:
@content = (0,1,3,4,5,6,7,8);
给定数字3,我希望数组擦除任何值为3且更少将数组@content
更改为:
@content = (4,5,6,7,8);
答案 0 :(得分:3)
使用grep
过滤值列表:
my @content = (1,2,3,4,5,6,7,8);
@content = grep {$_ > 3} @content;
print "@content\n";
输出:
4 5 6 7 8
答案 1 :(得分:0)
如果您愿意一路复制,请调查grep
。否则:
my @content = (1,2,3,4,5,6,7,8);
my $deleteUpTo = 3;
for ( my $i = @content - 1; $i >= 0; --$i )
{
if ($content[$i] <= $deleteUpTo)
{
splice(@content, $i, 1);
}
}
print join(', ', @content);
4,5,6,7,8