Perl删除变量中字符串后面的所有文本

时间:2014-10-02 19:19:26

标签: arrays perl

我有一个包含数字的数组:

@content = (0,1,3,4,5,6,7,8);

给定数字3,我希望数组擦除任何值为3且更少将数组@content更改为:

@content = (4,5,6,7,8);

2 个答案:

答案 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