Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i just don't know what command to use on this one.
$string = 1000000000000100000000000000100


what's the command to count how many bits "after each" 1
from $string

and put it a variable like
$var1 would get 30 bits
$var2 would get 17 bits
$var3 would get 2 bits

Replies are listed 'Best First'.
Re: binary problem
by dragonchild (Archbishop) on Mar 07, 2002 at 22:16 UTC
    Try:
    my @counts = map { length } $string =~ /1(0*)/g;
    Basically, capture all the zeros, then map the length function on each, and return that.

    Update: To get a count of characters after each 1, you could do something like

    my @counts = map { length } $string =~ /1(?=(.*))/g;
    We want to use the greediness of .*, so we do. We use look-ahead so that we can count the 1's that follow, but not eat them.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: binary problem
by rjray (Chaplain) on Mar 07, 2002 at 22:16 UTC

    The code below will do this, but rather than putting it into named variables like you say, it uses an array. One would have to use symbolic references (or an array of references that went to an arbitrary length) otherwise.

    $str = '1000000000000100000000000000100'; while (($pos = index($str, '1', ($pos ? $pos + 1 : 0))) >= 0) { push(@length, length($str) - $pos - 1); $pos++; } for (1 .. @length) { print "\$var$_ would get $length[$_-1] bits\n"; }

    When run, this gives me:

    $var1 would get 30 bits
    $var2 would get 17 bits
    $var3 would get 2 bits
    

    --rjray

Re: binary problem
by Corion (Patriarch) on Mar 07, 2002 at 22:00 UTC

    The idea is quite simple :
    Just split up the string at every 1 and then count how long the sequences of zeroes are that you got back. As a one-liner, this looks like

    perl -e "print length,qq(\n) for split /1/,'10000000000001000000000000 +00100';"

    As a full-blown Perl program, this would look somewhat like this :

    #!/usr/bin/perl -w use strict; while (<DATA>) { my @bits = split /1/; # every string starts with a 1, so # pop the first zero off of @bits pop @bits; print join( ",", @bits ), "\n"; }; __DATA__ 1000000000000100000000000000100
    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

      That won't produce the output he wanted. He wants the length of the string remaining from each 1 to the end of the string. This only counts the number of 0's between the 1's.

      --rjray

Re: binary problem
by Dog and Pony (Priest) on Mar 07, 2002 at 22:05 UTC
    Here you go:
    my $string = "1000000000000100000000000000100"; my $count = 0; $string =~ s/1/eval('$var' . ++$count . q(=length$'))/eg; print "$var1\n", "$var2\n", "$var3\n";
    Not that I would actually use that kind of hacky code in production, but... :)

    Disclaimer: this is just some for-fun code. We need more laughs. It does do the work though. :)


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.