in reply to Re: array pop
in thread array pop

thank you, how can we find which elements have deleted? how can we save it in an array? thank you very much

Replies are listed 'Best First'.
Re^3: array pop
by MidLifeXis (Monsignor) on Sep 10, 2012 at 13:54 UTC

    It depends. Are there any other requirements that you are going to dribble out?

    I would not use the approach I gave if I wanted to save what was removed. I would probably use splice instead. Additional requirements may change that answer.

    --MidLifeXis

Re^3: array pop
by Athanasius (Archbishop) on Sep 10, 2012 at 13:59 UTC

    Your questions are a little vague, but maybe this will help:

    #! perl use strict; use warnings; my @array = ('a' .. 'e'); print 'Original @array contents: ', join(', ', @array), "\n"; my @popped; unshift @popped, pop @array for 1 .. 3; print '@array now contains: ', join(', ', @array), "\n"; print 'The deleted elements: ', join(', ', @popped), "\n";

    Output:

    Original @array contents: a, b, c, d, e @array now contains: a, b The deleted elements: c, d, e

    See also Data: Arrays.

    Athanasius <°(((><contra mundum

      or more clearly, concisely and efficiently:

      #! perl use strict; use warnings; my @array = ('a' .. 'e'); print 'Original @array contents: ', join(', ', @array), "\n"; my @popped = splice @array, 0, 3; print '@array now contains: ', join(', ', @array), "\n"; print 'The deleted elements: ', join(', ', @popped), "\n";

      splice is your friend.

      True laziness is hard work