Perl中删除数组元素的“正确”方法是什么?

2019-05-2320:42:54后端程序开发Comments3,048 views字数 306阅读模式

我有一个包含一组元素的数组.元素的顺序是无关紧要的 – 我使用数组,因为它是我在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;
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/12815.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/12815.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定