my @arr = qw (and you will be pleased with the results now we have it working ); # 1 0 1 0 1 0 1 1 0 1 0 0 1 my $mask = 0b1010101101001; # For clarity # $mask = 0b0001010101101001; # Force to byte boundary # $mask = 0x1569; # ...if you prefer to think in nybbles my @result = wave_hands( $mask, @arr ); my $n = @arr; printf("The array contains $n elements\n"); print "@result"; # "and will pleased the results we working" 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; } # end wave_hands