Solved

adding a comma to numeric value xml/perl

Posted on 2007-03-29
5
205 Views
Last Modified: 2010-04-16
I have an xml variable in a script that hols a value like 2084. I want to convert it into 2,084. (put a comma in it) How do I do it?  Using Perl?

Thanks.
0
Comment
Question by:anup001
5 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 300 total points
ID: 18821257

$num =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;

0
 

Author Comment

by:anup001
ID: 18821295
what is the input variable here?
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 300 total points
ID: 18821355
$num is the variable.

So if you have

my $num=2084;
$num =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
print "Num is $num\n";

The output will be

Num is 2,084

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 150 total points
ID: 18821739
perldoc -q "How can I output my numbers with commas added"
Found inperlfaq5.pod
       How can I output my numbers with commas added?

       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;

       It is easier to see with comments:

          s/(
              ^[-+]?            # beginning of number.
              \d{1,3}?          # first digits before first comma
              (?=               # followed by, (but not included in the match) :
                 (?>(?:\d{3})+) # some positive multiple of three digits.
                 (?!\d)         # an *exact* multiple, not x * 3 + 1 or whatever.
              )
             |                  # or:
              \G\d{3}           # after the last group, get three digits
              (?=\d)            # but they have to have more digits after them.
          )/$1,/xg;
0
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 50 total points
ID: 18822468
Yet another method:
#!/usr/bin/perl -w
use strict;

my $number = "1234567.00";
$number = commify($number);

print "$number\n";

sub commify {
    my $input = shift;
    $input = reverse $input;
    $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g;
    $input =  reverse $input;
    return $input;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
modify powershell script to not inherit ntfs permission from parent 11 68
Python 2.7 - French characters 6 59
inactive users 13 71
How can I do this in Pyhton? 12 116
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now