The code I have works well, but I'm not entirely happy with it, so I would be glad to learn how it can be improved (shorter/clearer). This is a bit like a "programming exercise" I made up for myself, although I stumbled over this problem when writing a production program.

Problem: I have an even-sized array, with the property that the odd-numbered elements are never undef. This array should be processed left-to-right, two elements at a time, in a loop. The elements to be processed should be removed from the list. My solution goes like this:

# Array to be processed: @arr while(defined( ( my ($x,$y) = (shift(@arr), shift(@arr)) )[0])){ .... }
In this code, I combine the extraction of the first two elements from the list, with testing whether the list is already empty (which would set $x and $y to undef). Since the even-sized elements in the list are allowed to be undef, I test only $x.

However, I don't like the clumsy defined((my (...) ...)[0]). I would prefer a solution which looks as clean as the <c>each generator used to iterate over the elements of a hash. If I had this type of problem more frequently, I would perhaps write my own generator, but it would be overkill in this case. Maybe there is any other way I could improve my code?
-- 
Ronald Fischer <ynnor@mm.st>

In reply to Processing pairs of values from even-sized array by rovf

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.