Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Perl regular expression broken after move to 11.23

Highlighted
Frequent Advisor

Perl regular expression broken after move to 11.23


Old machine: HP-UX 11.11, Perl 5.8.5, possibly compiled it myself (I don't remember).

New machine: HP-UX 11.23, Perl 5.8.8 that came with the OS.

Regular expression is used in a short routine to put commas in a large dollar-value number:

$value =~ m/(-)??(\d{1,3})(\d\d\d)??(\d\d\d)??(\d\d\d)??(\d\d\d)??(\.\d\d)??$/;
$out = "${2},$3,$4,$5,$6${7}";
$out =~ s/,,+/,/g;
$out =~ s/,\././g;

The line starting with "$value" doesn't work correctly on the new machine. For example, input of 123465.78 gives output of "$123,"

Any ideas?

5 REPLIES
Frequent Advisor

Re: Perl regular expression broken after move to 11.23

Found a different RE on a website that will do the job. Don't know why this one doesn't work anymore.

Onward!

Acclaimed Contributor Acclaimed Contributor

Re: Perl regular expression broken after move to 11.23

Hi Chris:

> input of 123465.78 gives output of "$123,"

Your code works for me on Perl v5.8.8 built for IA64.ARCHREV_0-thread-multi-LP64 on an 11.23 machine of mine.

There are much more reliable and robust ways to inject commas into integers or decimal numbers. Do you have what you want?

Regards!

...JRF...



Trusted Contributor Trusted Contributor

Re: Perl regular expression broken after move to 11.23

# this subroutine adds commas to a number
#
sub commify {
my $input = shift;
$input = reverse $input;
$input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g;
return reverse $input;
}
Acclaimed Contributor Acclaimed Contributor

Re: Perl regular expression broken after move to 11.23

Hi (again):

The algorithm posted by mypel handles integer and decimal input. Another variation works well for integers and decimal numbers with no more than three decimal places:

# perl -le '$i=shift;1 while ($i=~s/(\d)((\d\d\d)+\b)/$1,$2/g);print $i'

Regards!

...JRF...
Frequent Advisor

Re: Perl regular expression broken after move to 11.23

Yes, I did get a regular expression that works ok. I had to modify it a bit to handle negative numbers. But I am up and running.

Thanks everyone for the help.

Chris