Solved

adding a comma to numeric value xml/perl

Posted on 2007-03-29
5
214 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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

623 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