| Newsgroups |
perl.cvs.perlfaq |
| Message-ID |
<[email protected]> |
cvsuser 02/08/21 17:05:50
Modified: . perlfaq5.pod
Log:
* How can I output my numbers with commas added?
+ a lot of people have asked me to un-delete the original subroutine
+ a couple of english fixes
Revision Changes Path
1.22 +11 -3 perlfaq/perlfaq5.pod
Index: perlfaq5.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq5.pod,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -w -r1.21 -r1.22
--- perlfaq5.pod 17 Jul 2002 17:51:36 -0000 1.21
+++ perlfaq5.pod 22 Aug 2002 00:05:50 -0000 1.22
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq5 - Files and Formats ($Revision: 1.21 $, $Date: 2002/07/17 17:51:36 $)
+perlfaq5 - Files and Formats ($Revision: 1.22 $, $Date: 2002/08/22 00:05:50 $)
=head1 DESCRIPTION
@@ -288,11 +288,19 @@
=head2 How can I output my numbers with commas added?
-This one from Benjamin Goldberg will do it for you:
+This subroutine will add commas to your number:
+
+ sub commify {
+ local $_ = shift;
+ 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
+ return $_;
+ }
+
+This regex from Benjamin Goldberg will add commas to numbers:
s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
-or written verbosely:
+It is easier to see with comments:
s/(
^[-+]? # beginning of number.