D
Dave Saville
Yesterday I needed to be able to print large numbers with commas. ie
1,234,567
Google turned up the following:
1 while s/^(-?\d+)(\d{3})/$1,$2/;
Two questions arise from this.
1) I have not seen the "1 while........" construct before - How does
that work? Or is it just syntatic suger to avoid empty curlies?
2) How does that regex actually work? I get the followed by three
digits bit - but its the -?\d+ bit I don't understand. And my camel
book does not mention -? Nor does perlre. I am guessing that most of
it is "look for at least one digit followed by exactly three digits.
Change to what you found first, a comma, and then the three digits.
The while keeps it going until the regex does nothing. I ran a few
tests and it makes no difference if the -? is there or not. Or at
least I could find no value where it did. The output is the same.
TIA
1,234,567
Google turned up the following:
1 while s/^(-?\d+)(\d{3})/$1,$2/;
Two questions arise from this.
1) I have not seen the "1 while........" construct before - How does
that work? Or is it just syntatic suger to avoid empty curlies?
2) How does that regex actually work? I get the followed by three
digits bit - but its the -?\d+ bit I don't understand. And my camel
book does not mention -? Nor does perlre. I am guessing that most of
it is "look for at least one digit followed by exactly three digits.
Change to what you found first, a comma, and then the three digits.
The while keeps it going until the regex does nothing. I ran a few
tests and it makes no difference if the -? is there or not. Or at
least I could find no value where it did. The output is the same.
TIA