Hello,
I am looking at the "Insert comma's into a number" script in Oreily's Perl
Cookbook (pg 64) and have some questions. The script (more or less) is below followed
by some output and questions:
#!/usr/bin/perl
my $num = reverse $ARGV[0];
$num =~ s/([0-9]{3}) (?=\d) (?!=\d*\.)/$1,/xg;
print scalar reverse $num;
print "\n";
exit(0);
>./foo.pl 10000
10,000
This is normal.
>./foo.pl 10,000,000
10,000,000
Why didn't it print 10,,000,,000?
>./foo.pl 10000.000
10,000.000
This is expected.
>foo.pl 10000.000000
10,000.000,000
Why didn't (?!=\d*\.) catch this?
To help prevent the last problem, is there a way to match "only 2 digits if
there is a period", or "if there is a period, make sure there are only two digits"? I
am dealing with dollars and cents, so it's only specific two digits. I would like to
have the option of entering "1,000" instead of "1,000.00" also. The problem I am
having looking for this regexp is that if I put "[0-9]{0,2}\.?", "10.0000" fails since
the period is optional. I guess I could use printf to change 10.0000 into 10.00, but I
would like to produce an error incase of mistypes instead of destroying data.
Thanks,
=-= Robert Thompson
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]