views:

90

answers:

2

Hi, 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];.

+5  A: 

Use splice() :

splice @$a, 1, 1;
eugene y
+4  A: 

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.

daxim
I'm ok with the types, but forget to name them correctly, since I only have to write Perl by accident ;) Correcting the question.
viraptor