我有一个包含一组元素的数组.元素的顺序是无关紧要的 - 我使用数组,因为它是我在Perl中知道的最简单的数据结构.
my @arr = ...
while (some condition) {
# iterate over @arr and remove all elements which meet some criteria
# (which depends on $i)
}
Run Code Online (Sandbox Code Playgroud)
我知道,splice()但我认为在迭代时使用它并不好.delete对于数组元素似乎已弃用.也许用grep在@arr为自己(@arr = grep {...} @arr)?
这里的最佳做法是什么?
也许使用哈希(虽然我真的不需要它)?
根据文档,delete不推荐使用调用数组值,并且可能会在未来的Perl版本中删除它们.
或者,您可以构建所需索引的列表并将切片分配给原始数组:
@arr = @arr[ @indices ];
Run Code Online (Sandbox Code Playgroud)
您可以在perldata中阅读有关切片的更多信息.