小编Sha*_*awn的帖子

如何删除引用数组的元素?

我想用一个子程序从几个大数组中删除元素.我使用引用来避免副本进入子.

@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 …
Run Code Online (Sandbox Code Playgroud)

arrays perl pass-by-reference

5
推荐指数
2
解决办法
2920
查看次数

标签 统计

arrays ×1

pass-by-reference ×1

perl ×1