Lol, you've got to love a refresher in middle-school math. ;) Thanks.
Taking that into consideration, and adding a built-in string validity check, here's how the working code now looks:
use strict; use warnings; use Carp; while ( <DATA> ) { chomp; print "$_\t=>\t", bin_to_dec($_), "\n"; } sub bin_to_dec { our $num; # $num must be a global to work. local $num; # Play nice if $num is already in global use. $_[0] =~ m/ (?=^[10]+$) # Check for a valid string. (?{ $num = 0; }) (?: ([10]) (?{ $num = 2 * $num + $^N; }) )+ /x or croak "Error: $_[0] is not a pure bit string.\n"; return $num; } __DATA__ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Thanks again...
Dave
In reply to Re^2: Inexplicable uninitialized value when using (?{...}) regexp construct.
by davido
in thread Inexplicable uninitialized value when using (?{...}) regexp construct.
by davido
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |