http://qs1969.pair.com?node_id=474794

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

What is the difference between these three funktions? Pop, shift and delete. Pop is opposite of push, shift of unshift and delete stands for its own. But they do all the same in my opinion. Or I am wrong and there are some subtle differences. If so, what are these differences? Thank you very much.

Replies are listed 'Best First'.
Re: Pop/shift/delete on array
by Zaxo (Archbishop) on Jul 14, 2005 at 08:37 UTC

    The push and pop functions act at the high-index end of an array. Shift and unshift act on the low-index end.

    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. For arrays, splice is the corresponding operator.

    After Compline,
    Zaxo

      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
      on an array it only undefines the value of an element

      Actually, it does a bit more than "only" undefining the value. I'm not sure how useful this behavior is, but it's not quite the same as simply setting the value to undef. Here's a code snippet that demonstrates:

      $ perl -MData::Dumper -le ' @array = (1 .. 3); delete $array[1]; print Dumper \@array; delete $array[2]; print Dumper \@array;'

      When run, it outputs the following:

      $VAR1 = [ 1, undef, 3 ]; $VAR1 = [ 1 ];

      We see that the first delete call does indeed appear to simply set the value to undef, but when we delete again, it demonstrates the difference.

      Update: betterworld beat me to it, by a pretty large margin. That'll teach me not to reload before replying. :-)

Re: Pop/shift/delete on array
by monarch (Priest) on Jul 14, 2005 at 08:34 UTC

    let's say you have an array that looks like this

    my @vals = ( 'cat', 'dog', 'mouse' );

    Let's push onto this array:

    push( @vals, 'tiger' ); print( join(',',@vals ) );
    outputs cat,dog,mouse,tiger.

    Let's pop from this array:

    print( pop( @vals ) );
    outputs tiger.

    Let's shift from this array:

    print( shift( @vals ) );
    outputs cat.

    Note that both shift and pop remove the value from the array that you obtained:

    print( join(',',@vals ) );
    prints dog,mouse.

    Finally delete isn't an array function, it's used for deleting a hash element... update: whoops Limbic~Region has pointed out I was wrong about the delete function.. read his comments below.

      monarch,
      Finally delete isn't an array function, it's used for deleting a hash element.

      While I have yet to see a good use for using delete on an array, this is misinformation. The docs for delete clearly indicate it can be used for arrays and array slices though it behaves differently then if it had been performed on a hash. It is designed to work with exists on array elements which I find just as icky. If the element isn't at the either end, I prefer to use splice to get rid of it even though I hear the cost of splicing an array is proportional to the size of the array at time of splicing.

      Cheers - L~R

Re: Pop/shift/delete on array
by Anonymous Monk on Jul 14, 2005 at 08:58 UTC
    Thank you for the examples and explanations. High, low index and undefine of arrays elements. I will remember that.