If Perl had a =>> operator, the code would look like this:

perl -le "$mask = 0x2b; print for map {$mask =>> 1?$_:()} @ARGV" and y +ou will be pleased with the results

... but as Perl lacks shift-and-assign operators that return the shifted-off bits, my code is uglier:

perl -le "$mask = 0x2b; print for map {my $sel = $mask & 1; $mask = $m +ask >> 1; $sel?$_:()} @ARGV" and you will be pleased with the results and you be with

Update: I found >>= and <<=, but they don't seem to act like I'd want, namely returning the shifted-off bits. In fact, my experiments don't make it clear to me what the operators do, as they leave $mask unchanged it seems:

perl -le "$mask = 0x2b; print for map {$mask >>= 1?$_:();} @ARGV" and +you will be pleased with the results

prints

43 43 43 43 43 43 43 43

... which I don't really understand.

Update 2: D'oh. Rereading the documentation, I now understand that $mask >>= 1 is equivalent to $mask = $mask >> 1, which returns the (new) value of $mask instead of returning the shifted-off bits. So it's not as usable as I'd need, for this case of golfing.


In reply to Re: Taking arbitrary elements from an array by Corion
in thread Taking arbitrary elements from an array by grinder

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.