I disagree that my loops aren't equivalent. It is common practice in C to write loops of the form
for( $i = 0; $i <= $foo; $i++ ) { last if bar( $foo ); } if( $i > $foo ) { # Condition never met! }
Essentially, what I would like to do is distinguish between bar(..) being true for the last element in my last, and never being true at all. In "my perfect little world", I would like foreach to unset its iterator variable if and only if it loops through the entire list without encountering last.

So, in my example above, if bar( $foo ) is the first to be true, I want $i == $foo; if bar(..) was never true, it'd be nice to have $i be undef.

In addition, if bar(..) has side effects, then our code is not equivalent.

Edit: Also, I prefer a foreach-style loop because all I'm doing is iterating -- precisely the designated purpose of foreach. Otherwise, if I have a long variable name, I would have to type for( $long_variable_name = 1; $long_variable_name <= 5; $long_variable_name ++ ) versus for $long_variable_name ( 1 .. 5 ). I'm pretty sure I'm not the only one who has made copy-paste errors like for( $j = 0; $j <= 5; $i++ ).


In reply to Re^3: Last undefines a for loop's itererator? by kaif
in thread Last undefines a for loop's itererator? by BUU

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.