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

How do I loop through all elements in a array except the first?
  • Comment on Loop through all elements in a array except the first

Replies are listed 'Best First'.
Re: Loop through all elements in a array except the first
by jethro (Monsignor) on Mar 18, 2009 at 15:19 UTC
    foreach (@array[1..$#array]) { ... }

    its called an array slice

Re: Loop through all elements in a array except the first
by kennethk (Abbot) on Mar 18, 2009 at 15:21 UTC
Re: Loop through all elements in a array except the first
by locked_user sundialsvc4 (Abbot) on Mar 18, 2009 at 16:33 UTC

    TMTOWTDI: There's More Than One Way To Do It.

    Therefore... what really matters? Only this ... that whatever code you write, you make your intentions perfectly clear.

    Don't be unnecessarily tripped-up by thoughts of “efficiency.” Probably, no matter how you decide to do it, that won't be an issue. Just choose a strategy that obviously works, and add a short comment alerting the Next Gentle Reader of what you've done. (Even if that “Next Gentle Reader” is yourself.)

Re: Loop through all elements in a array except the first
by targetsmart (Curate) on Mar 18, 2009 at 15:16 UTC
    shift the array before the loop and unshift after the loop like this.
    $firstelement = shift @array; for (@array){ do something... } unshift(@array,$firstelement);

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Loop through all elements in a array except the first
by Anonymous Monk on Mar 18, 2009 at 15:27 UTC
    Skip the 1st one, or start from the 2nd one. perlintro