Solved

adding a comma to numeric value xml/perl

Posted on 2007-03-29
5
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Recursively Delete Files 5 103
Log File Creation with Header and Footer 17 154
Powershell File Sort 8 45
Perl script to process a .csv file 18 59
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
This article will show, step by step, how to integrate R code into a R Sweave document
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 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