如何删除引用数组的元素? [英] How can I delete an element of a referenced array?

查看:97
本文介绍了如何删除引用数组的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个子程序几大数组删除元素。我用一个参考,以避免进入副本子。

I want to remove elements from a few large arrays with a subroutine. I use a reference to avoid a copy into the sub.

@a=qw(ok now what is hi the matter);

sub zonk {
  $array=shift; # this is a reference of an array
  foreach $i (0..$#$array) { # I saw some say to avoid last element to get size
    #if (@$array[$i] =~ /hi/) { delete @$array[$i]; }
    #if ($array->[$i] =~ /hi/) { delete $array->[$i]; }
    #if ($array->[$i] =~ /hi/) { delete @$array->[$i]; }
    if ($array->[$i] =~ /hi/) { print "FOUND "; }
    print $array->[$i],"\n";
  }
  @$array = grep{$_} @$array; # removes empty elements
}
zonk(\@a);
print join(':',@a);

如果我运行上面的程序我得到:

If I run the program above as is I get:

ok
now
what
is
FOUND hi
the
matter
ok:now:what:is:hi:the:matter

但是,如果我使用注释行,而不是任何我得到:

But if I use any of the commented lines instead I get:

删除参数不是在hi.pl线10 HASH元素或切片。

delete argument is not a HASH element or slice at hi.pl line 10.

我试过拼接原本但随后的指标进行转换和困惑的迭代。
将是很好知道在这个帖子中提到的所有方法,但最有效的就是我在寻找:)

I tried splice originally but then the indices were shifting and confused the iteration. Would be nice to know all the methods mentioned in this post, however the most efficient is what I am looking for :)

附录:这工作完全(我的意思是每一个注释行)我的Linux机器(Ubuntu的9.10,PERL 5.10)上,但上面的错误是用perl 5.005_03在工作中我的Windows 7盒。
升级是不是一种选择。

Addendum: This works perfectly (I mean every commented line) on my linux machine (ubuntu 9.10, perl 5.10) but the above error is on my Windows 7 box at work using perl 5.005_03. Upgrading is not an option.

感谢

推荐答案

为什么不能用grep从一开始走?

Why not grep from the get-go?

@array = grep { !/hi/ } @array;
# Or, for *referenced* array
@$arrayRef = grep { !/hi/ } @$arrayRef;

一个小套的音符澄清,评论出现问题:

A small set of notes to clarify questions that arose in comments:


  1. 这个方法(或使用的grep 包括原始的海报的code的任何方法)的增加了脚本的内存使用情况由新生成的阵列的大小。

  1. This method (or any method using grep including original poster's code) will increase the memory usage of the script by the size of the newly resultant array.

例如。如果脚本(SANS第一阵列)讨论了10MB的内存,原始数组了内存15MB,所得阵列了内存14MB,那么你的程序的总内存占用的将增加从25MB到39MB,而的grep 正在运行。

E.g. if the script (sans the first array) took up 10MB of memory, the original array took 15MB of memory, and the resulting array took 14MB of memory, then the total memory footprint of your program will increase from 25MB to 39MB while grep is running.

一旦的grep comlpetes,由原始数组所使用的内存将成为可用于垃圾回收(有一些注意事项无关的这一职务)。

Once the grep comlpetes, the memory used by the original array will become available for garbage collection (with some caveats irrelevant to this post).

然而 - 这很重要 - 即使数据的原始15MB的垃圾回收,的15MB不会被Perl返回到操作系统 - 例如脚本的内存占用的将保持39MB ,甚至垃圾回收后不会下降到24MB。

However - and this is important - even if the original 15MB of data are garbage collected, that 15MB will not be returned by Perl to operating system - e.g. the script's memory footprint will remain 39MB and won't drop to 24MB even after garbage collection.

在好的一面,也释放出15MB将可用于整个项目的生命周期其余的内存分配(姑且内存碎片问题) - 因此,如果你的脚本需要其它附加1MB的分配,5MB ,或15MB内存,其内存占用量不得超出39MB的高点。而如果它需要额外的内存17MB,由此产生的内存占用仅只有41MB,56MB不是

On the good side, that freed-up 15MB will be available for memory allocation throughout the rest of your program's lifetime (leaving aside memory fragmentation issues) - therefore, if your script will require allocation of additionnal 1MB, 5MB, or 15MB of memory, its memory footprint will NOT grow beyond the high-point of 39MB. And if it requires 17MB of additional memory, the resulting memory footprint will only be only 41MB, not 56MB.

如果此内存算术不尽如人意你(例如,如果你的原始数组是500MB,你是不是愿意容忍程序的内存占用上升到1GB),那么<一个href=\"http://stackoverflow.com/questions/4415287/how-can-i-delete-an-element-of-a-referenced-array/4415420#4415420\">Dallaylaen's下面回答是做任务的一个伟大的算法,无需额外内存分配

If this memory arithmetic is not satisfactory to you (e.g. if your original array was 500MB and you aren't willing to tolerate the program memory footprint rising to 1GB), then Dallaylaen's answer below is a great algorithm for doing the task without extra memory allocation

这篇关于如何删除引用数组的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆