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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |