我有一个包含一组元素的数组.元素的顺序是无关紧要的 – 我使用数组,因为它是我在Perl中知道的最简单的数据结构.文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12815.html
my @arr = ... while (some condition) { # iterate over @arr and remove all elements which meet some criteria # (which depends on $i) }
我知道splice()但我认为在迭代时使用它并不好.删除数组元素似乎已弃用.也许在@arr上使用grep(@arr = grep {…} @arr)?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12815.html
这里的最佳做法是什么?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12815.html
也许使用哈希(虽然我真的不需要它)?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12815.html
解决方法
你使用grep的想法很好文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12815.html
@arr = grep { cond($i++); } @arr;