in reply to Re: Pop/shift/delete on array
in thread Pop/shift/delete on array
The delete operator doesn't really belong in the list. It removes pairs from a hash, but on an array it only undefines the value of an element.This is not quite true. If you delete the last element of an array, the array will actually shrink:
You can even have it funnier:my @array = qw/tiger dog cat/; delete $array[2]; print scalar @array; # Output is 2
However, this seemingly equivalent code does behave weird:my @array = qw/tiger dog cat/; delete $array[1]; print scalar @array; # Output is 3 delete $array[2]; print scalar @array; # Output is 1
my @array = qw/tiger dog cat/; delete $array[1]; print scalar @array; # Output is 3 splice @array, 2; print scalar @array; # Output is 2
|
---|
In Section
Seekers of Perl Wisdom