Re: Perl regular expression broken after move to 11.23 (110 Views)
Reply
Frequent Advisor
Chris Howard
Posts: 40
Registered: ‎02-15-1999
Message 1 of 6 (110 Views)

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?

Please use plain text.
Frequent Advisor
Chris Howard
Posts: 40
Registered: ‎02-15-1999
Message 2 of 6 (110 Views)

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!

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 6 (110 Views)

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...



Please use plain text.
Trusted Contributor
mvpel
Posts: 271
Registered: ‎01-11-2009
Message 4 of 6 (110 Views)

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;
}
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 6 (110 Views)

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...
Please use plain text.
Frequent Advisor
Chris Howard
Posts: 40
Registered: ‎02-15-1999
Message 6 of 6 (110 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation