Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Pop/shift/delete on array

by betterworld (Curate)
on Jul 14, 2005 at 16:07 UTC ( #474932=note: print w/replies, xml ) Need Help??


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:
my @array = qw/tiger dog cat/; delete $array[2]; print scalar @array; # Output is 2
You can even have it funnier:
my @array = qw/tiger dog cat/; delete $array[1]; print scalar @array; # Output is 3 delete $array[2]; print scalar @array; # Output is 1
However, this seemingly equivalent code does behave weird:
my @array = qw/tiger dog cat/; delete $array[1]; print scalar @array; # Output is 3 splice @array, 2; print scalar @array; # Output is 2

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://474932]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2019-02-21 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I use postfix dereferencing ...









    Results (108 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!