As mentioned by others, that will fail if the array may contain false or undefined values. You can do something similar by keeping the entire assignment in list context and making sure to assign a list:

while( my ( $a ) = @a ? shift @a : () ) { # ... }

Note the parens around $a: they make all the difference. They put the my (and thus the entire expression) in list context, and so the boolean value depends on the number of elements assigned. The ternary operator contortion on the right side ensures that when @a is empty, an empty list is assigned, so the loop aborts correctly.

Try putting empty strings, zeros or undefs in the array: you’ll find it will always do exactly what it’s supposed to.

However, elegant is not what it is. I’d much rather use while( @a ) and do a separate shift – far fewer subtleties to contend with.

Makeshifts last the longest.


In reply to Re^2: Array Processing by Aristotle
in thread Array Processing by Rajeshk

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.