split

split

am 13.09.2004 00:12:03 von George Bouras

# I want to separate numbers by thousands. I use this
$val = reverse $val;
$val =~s/(\d{3})/$1\./g;
$val = reverse $val;
$val =~s/^\.*//;
# Is there any shorter way ?

Re: split

am 13.09.2004 01:50:08 von Gunnar Hjalmarsson

George Bouras wrote:
> # I want to separate numbers by thousands. I use this
> $val = reverse $val;
> $val =~s/(\d{3})/$1\./g;
> $val = reverse $val;
> $val =~s/^\.*//;
> # Is there any shorter way ?

I'm using this:

1 while $val =~ s/(\d)(\d\d\d)(?!\d)/$1.$2/g;

Picked it up once from the FAQ, I think, but don't find it there now.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: split

am 13.09.2004 11:10:28 von Tintin

"Gunnar Hjalmarsson" wrote in message
news:2qk5jhFvu0o3U1@uni-berlin.de...
> George Bouras wrote:
> > # I want to separate numbers by thousands. I use this
> > $val = reverse $val;
> > $val =~s/(\d{3})/$1\./g;
> > $val = reverse $val;
> > $val =~s/^\.*//;
> > # Is there any shorter way ?
>
> I'm using this:
>
> 1 while $val =~ s/(\d)(\d\d\d)(?!\d)/$1.$2/g;
>
> Picked it up once from the FAQ, I think, but don't find it there now.

perldoc -q comma

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 $_;
}

That's from Perl 5.8.0

Re: split

am 25.09.2004 06:08:53 von U-L-T-R-A

This regex will add commas no matter how long the number string is:

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

This returns: 1,000,000,000

------------------------------------------------------------ --------------------------
"Tintin" wrote in message
news:2ql6f0FvnutnU1@uni-berlin.de...
>
> "Gunnar Hjalmarsson" wrote in message
> news:2qk5jhFvu0o3U1@uni-berlin.de...
>> George Bouras wrote:
>> > # I want to separate numbers by thousands. I use this
>> > $val = reverse $val;
>> > $val =~s/(\d{3})/$1\./g;
>> > $val = reverse $val;
>> > $val =~s/^\.*//;
>> > # Is there any shorter way ?
>>
>> I'm using this:
>>
>> 1 while $val =~ s/(\d)(\d\d\d)(?!\d)/$1.$2/g;
>>
>> Picked it up once from the FAQ, I think, but don't find it there now.
>
> perldoc -q comma
>
> 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 $_;
> }
>
> That's from Perl 5.8.0
>
>