This will demonstrate why popping items off of the list you are iterating over is a bad idea:
use strict; use warnings; my @list = 'a'..'z'; for my $letter (@list) { my $popped = pop @list; printf "letter: %s popped: %s list: %s\n",$letter,$popped,join('', +@list); }
Output:
letter: a popped: z list: abcdefghijklmnopqrstuvwxy letter: b popped: y list: abcdefghijklmnopqrstuvwx letter: c popped: x list: abcdefghijklmnopqrstuvw letter: d popped: w list: abcdefghijklmnopqrstuv letter: e popped: v list: abcdefghijklmnopqrstu letter: f popped: u list: abcdefghijklmnopqrst letter: g popped: t list: abcdefghijklmnopqrs letter: h popped: s list: abcdefghijklmnopqr letter: i popped: r list: abcdefghijklmnopq letter: j popped: q list: abcdefghijklmnop letter: k popped: p list: abcdefghijklmno letter: l popped: o list: abcdefghijklmn letter: m popped: n list: abcdefghijklm
By the time you reach letter 'm' you have removed enough items from the end of the list that you are now at the new end.

Update - added quote from perlsyn

 If any part of LIST is an array, "foreach" will get very
 confused if you add or remove elements within the loop
 body, for example with "splice".   So don't do that

In reply to Re: Using Push and Pop. - response to update by imp
in thread Using Push and Pop. by dark314

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.