开发者

Removing array elements

how do I remove an element from a Perl array ref? I've got its index and I don't want to set the element to undef, but to remove it completely.

For example, how do I change $a = [1, 2, 3开发者_开发知识库]; into $a = [1, 3];.


Use splice() :

splice @$a, 1, 1;


Use splice:

my @array = (44,55,66);
splice @array, 1, 1; # 55
@array; # (44,66)

my $array_ref = [44,55,66];
splice @{ $array_ref }, 1, 1; # 55
$array_ref; # (44,66)

I have noticed that you are confused about data types and references because Devel::REPL displays them in the reference form only. You defined an array reference in the question, not an array. Compare my code examples.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜