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