in reply to Taking arbitrary elements from an array
Heres my go, although wave_hands() seems like a funny name for the routine... ;-)
sub wave_hands { my $mask=0+shift; my $i=1; my @r; for ( my $p=$#_ ; $p>=0 && $i<=$mask ; $p-- ) { unshift @r,$_[$p] if $mask & $i; $i<<=1; } return @r; } 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 ); print "@result"; # "will pleased the results"
If speed is a concern you shouldn't pass this stuff on the stack, use refs instead...
|
|---|