On Nov 9, Robert Thompson said:

>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:

You copied it incorrectly, or there is a typo in the book:

>       $num =~ s/([0-9]{3}) (?=\d) (?!=\d*\.)/$1,/xg;  

That should be (?! \d*\. ), not (?!= \d*\. ).

>       >./foo.pl 10,000,000
>       10,000,000
>       Why didn't it print 10,,000,,000?

Why would it double the commas?  The algorithm is this:

  10000000
    reverse the number
  00000001
    get the next three digits
  000xxxxx
    that are followed by another digit
  0000xxxx
    and that are not followed by any number of digits and a period
  (ok)
    place a comma after the three digits
  000,00001
    get the next three digits
  xxxx000xx
    that are followed by another digit
  xxxx0000x
    and that are not followed by any number of digits and a period
  (ok)
    place a comma after the three digits
  000,000,01
    get the next three digits
  (fail)
  (done)

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to