grinder has asked for the wisdom of the Perl Monks concerning the following question:
Here's something that might be fun for people with too much spare time to play around with :) Consider an array and an integer mask.
my @arr = qw (and you will be pleased with the results); my $mask = 0x2b; # 0 0 1 0 1 0 1 1 my @result = wave_hands( $mask, @arr ) # returns qw(will pleased the results)
That is, take some of the elements of the array according to whether the corresponding bit is set in $mask. If possible, without destroying @arr. Assume that no bit-value in $mask will point beyond the end of @arr.
I've been wondering what's the fastest way to achieve the result. I don't particularly mind if the mask gets applied "backwards", which would result in qw(and you be with) because at the very worst, I'd only need to reverse the original array to get the correct result.
The values of @arr could be unique, if using a hash would help, but in the general case, duplicates could occur. I can think of at least three techniques:
... Or there some really simple trick that does this without a whole lot of make-work code?
• another intruder with the mooring in the heart of the Perl
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Taking arbitrary elements from an array
by Corion (Patriarch) on Nov 28, 2005 at 14:18 UTC | |
by liverpole (Monsignor) on Nov 28, 2005 at 14:49 UTC | |
|
Re: Taking arbitrary elements from an array
by Fletch (Bishop) on Nov 28, 2005 at 14:28 UTC | |
|
Re: Taking arbitrary elements from an array
by demerphq (Chancellor) on Nov 28, 2005 at 14:47 UTC | |
|
Re: Taking arbitrary elements from an array
by holli (Abbot) on Nov 28, 2005 at 15:47 UTC | |
|
Re: Taking arbitrary elements from an array
by sauoq (Abbot) on Nov 28, 2005 at 16:03 UTC | |
|
Re: Taking arbitrary elements from an array
by ozboomer (Friar) on Feb 14, 2006 at 03:39 UTC | |
|
Re: Taking arbitrary elements from an array
by ozboomer (Friar) on Feb 15, 2006 at 09:25 UTC |