The well-known commifying snippet from the perlfaq5 is tricky enough to understand; the recent addition in that answer is much worse. And all of the other snippets I've seen use regex contortions to get the job done as well. Why?
The following is completely clear and straightforward, yet as I'm surprised to find, it seems noone else has posted something along these lines so far.
Update: of course it was supposed to be scalar reverse in both instances.
Update: this original version doesn't correctly handle anything but a string of digits:
sub commify { scalar reverse join ',', unpack '(A3)*', scalar reverse shift }
Update: gack, I need to sleep. Fixed three instances of reverse scalar that were supposed to be scalar reverse.
sub commify { my ( $sign, $int, $frac ) = ( $_[0] =~ /^([+-]?)(\d*)(.*)/ ); my $commified = ( scalar reverse join ',', unpack '(A3)*', scalar reverse $int ); return $sign . $commified . $frac; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Commify numbers, the boring and straightforward way
by holli (Abbot) on Mar 02, 2005 at 08:00 UTC | |
by Aristotle (Chancellor) on Mar 02, 2005 at 11:28 UTC | |
|
Re: Commify numbers, the boring and straightforward way
by jmcnamara (Monsignor) on Mar 02, 2005 at 08:53 UTC | |
by Aristotle (Chancellor) on Mar 02, 2005 at 09:14 UTC | |
by halley (Prior) on Mar 02, 2005 at 15:07 UTC | |
by Aristotle (Chancellor) on Mar 02, 2005 at 17:14 UTC | |
|
Re: Commify numbers, the boring and straightforward way
by eyepopslikeamosquito (Archbishop) on Mar 02, 2005 at 08:04 UTC |