in reply to Taking arbitrary elements from an array

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.

Replies are listed 'Best First'.
Re^2: Taking arbitrary elements from an array
by liverpole (Monsignor) on Nov 28, 2005 at 14:49 UTC
    Your second example also has a glitch due to precedence.  The expression:
    $mask >>= 1? $_: ();
    was intended, I believe, to be:
    ($mask >>= 1)? $_: ();
    but is actually equivalent to:
    $mask >>= (1? $_: ());
    This evaluates to true each time (1?) thus using each word ($_) as the value by which to shift $mask, and each of the strings evaluates to numerical zero.  That's why $mask never changes, and gives you a 43 (0x2b) resulting from the map applied to each value from @ARGV;

    @ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"