Don't they want to get a list "e.g. ( 1, 0, 0, 1, 1, 0 ... )" as stated in the beginning of that section,
Why not just try it out?
$vec = pack 'V', int( 2**32* rand() );; print $bits;; 011000001111110110010000101101 @ints = (); { use integer; push @ints, 0 if $bits =~ s/^(\d)// && $1; push @ints, pos $bits while $bits =~ /1/g; };; print @ints;; 1 2 8 9 10 11 12 13 15 16 19 24 26 27 29
The are producing a list of the positions of the bits that are set.
I do agree that the section title and description and the sub name are woefully bad.
I'm also less than impressed with the claims for efficiency:
C:\test>perl -MMath::Random::MT=rand -minteger -MBenchmark=cmpthese $vec = pack 'V*', map int( 2**32* rand() ), 1 ..10;; cmpthese -1, { a=>q[ my @ints; my $bits = unpack 'b*', $vec; use integer; push @ints +, 0 if $bits =~ s/^(\d)// && $1;push @ints, pos $bits while($bits =~ +/1/g); ], b=>q[ my @ints = grep{ vec( $vec, $_, 1 ) } 0 .. length( $vec ) -1; ] +, };; ^Z Rate a b a 22306/s -- -79% b 108646/s 387% --
In reply to Re: Confusion about code sample in perlfaq4
by BrowserUk
in thread Confusion about code sample in perlfaq4
by vr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |