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

Hi Monks, i wanna pop more than one time from an array.how can i do that without repeating the code.thank you

Replies are listed 'Best First'.
Re: array pop
by choroba (Cardinal) on Sep 10, 2012 at 13:00 UTC
    You can use splice
    splice @array, -$number;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: array pop
by MidLifeXis (Monsignor) on Sep 10, 2012 at 12:56 UTC

    pop(...) for ( 0 .. 5 );

    --MidLifeXis

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

        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

        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