Subject: | Odd regex behavior with bignum |
I'm using perl 5.10 on fedora 12, the latest version in the repos. I was
creating a perl script to complete problem 16 of project euler,
basically I have to go through each digit of a bignum and sum it up.
Below is the code I came up with...
#!/usr/bin/perl
use bignum;
my $num = 2**1000;
my $sum = 0;
while ($num =~ m/(\d{1})/g){
$sum += int($1);}
print "$sum";
This doesn't work beacause the first few digits of the regex match are
strange or incorrect characters. Eventually this solution worked:
#!/usr/bin/perl
use bignum;
my $num = 2**1000;
my $sum = 0;
print "$num\n";
while ($num =~ m/(\d{1})/g){
print $1;
$sum += int($1);}
print "$sum";
I had to have those print statements in that order to work...even if I
got rid of the new line after $num it wouldnt work. Or if I got rid of
print $1 it wouldnt work. The strangest part is if I had a \n symbol
before $sum the regex wouldnt work, even though it was after the
matching loop. I know this seems bizarre but this is the only way I
could get it to work.