replace string with variable

A

aca

Hi, from Spain, I'm new user in perl, I have a problem with regular
expression,

I dont know how I can replace the zeros on the right of the decimal
separator, for example I have a file with some lines of data how this
example:

20.1300,20.2500,19.7700,20.2500,985365,0

And I want that the final text appear how this

20.13,20.25,19.77,20.25,985365,0

I try use the regular expression but the final patron I dont know how
it can
be variable.

Thanks for your help.

ACA
 
M

Mirco Wahab

Thus spoke aca (on 2006-05-29 12:06):
I dont know how I can replace the zeros on the right of the decimal
separator, for example I have a file with some lines of data how this
example:
20.1300,20.2500,19.7700,20.2500,985365,0

And I want that the final text appear how this

20.13,20.25,19.77,20.25,985365,0

my first shot: use split //, $text
and join the Elements then:

...
# have a file with some lines of data how this example:
my $text = qq{20.1300,20.2500,19.7700,20.2500,985365,0};

my @fields = split /(?<=[^0])0*,/, $text;
my $new_text = join ',', @fields;

# And I want that the final text appear how this
# 20.13,20.25,19.77,20.25,985365,0
print $new_text, "\n";
...

prints: 20.13,20.25,19.77,20.25,985365,0

(There are zillions of other variants how to do that ..)

Regards

Mirco
 
L

Lars Haugseth

|
| Hi, from Spain, I'm new user in perl, I have a problem with regular
| expression,
|
| I dont know how I can replace the zeros on the right of the decimal
| separator, for example I have a file with some lines of data how this
| example:
|
| 20.1300,20.2500,19.7700,20.2500,985365,0
|
| And I want that the final text appear how this
|
| 20.13,20.25,19.77,20.25,985365,0

This ought to do the trick:

s/(\.\d+?)0+(?!\d)/$1/g;
 
G

Gunnar Hjalmarsson

aca said:
I have a file with some lines of data how this example:

20.1300,20.2500,19.7700,20.2500,985365,0

And I want that the final text appear how this

20.13,20.25,19.77,20.25,985365,0

s/(\.\d*[1-9])0+\b|\.0*\b/$1 or ''/eg;
 
A

axel

aca said:
I dont know how I can replace the zeros on the right of the decimal
separator, for example I have a file with some lines of data how this
example:

And I want that the final text appear how this
20.13,20.25,19.77,20.25,985365,0

$str =~ s/0+,/,/g;
I try use the regular expression but the final patron I dont know how
it can
be variable.

I am not sure exactly what you mean.

Axel
 
M

Mirco Wahab

Thus spoke Gunnar Hjalmarsson (on 2006-05-29 12:53):
s/(\.\d*[1-9])0+\b|\.0*\b/$1 or ''/eg;

$text = "20.01";
$text =~s/(\.\d*[1-9])0+\b|\.0*\b/$1 or ''/eg;
print "$text\n";

==> 2001

Regards

Mirco
 
M

Mirco Wahab

Thus spoke Lars Haugseth (on 2006-05-29 12:50):
This ought to do the trick:

s/(\.\d+?)0+(?!\d)/$1/g;

$text = "200.,200.0000";
$text =~ s/(\.\d+?)0+(?!\d)/$1/g;
print "$text\n";

=> 200.,200.0

Regards

Mirco
 
M

Mirco Wahab

Thus spoke (e-mail address removed) (on 2006-05-29 12:56):
$str =~ s/0+,/,/g;
$text = "200.,200.0000";
$text =~ s/0+,/,/g;
print "$text\n";

==> 200.,200.0000

Regards

Mirco
 
C

cmic

Mirco Wahab a écrit :
Thus spoke aca (on 2006-05-29 12:06):
I dont know how I can replace the zeros on the right of the decimal
separator, for example I have a file with some lines of data how this
example:
20.1300,20.2500,19.7700,20.2500,985365,0

And I want that the final text appear how this

20.13,20.25,19.77,20.25,985365,0

my first shot: use split //, $text
and join the Elements then:

...
# have a file with some lines of data how this example:
my $text = qq{20.1300,20.2500,19.7700,20.2500,985365,0};

my @fields = split /(?<=[^0])0*,/, $text;

Or even like this (negate + negate => equate !)
my @fields = split /(?<!0)0*?,/, $text;
my $new_text = join ',', @fields;
.....

(There are zillions of other variants how to do that ..)

There are now zillions minus one ...
 
M

Mirco Wahab

Thus spoke cmic (on 2006-05-29 13:07):
Or even like this (negate + negate => equate !)
my @fields = split /(?<!0)0*?,/, $text;

No, wont work:

$text = "200.,200.0000";
my @fields = split /(?<!0)0*?,/, $text;
my $text = join ',', @fields;
print $text;

==> 200.,200.0000

Regards

Mirco
 
A

aca

The first thanks to all for yours rapid answers.

Lars I try you solution , because it the first that I understand, but
the final results
are :

20,20,19,20 and not 20.13,20.25,19.77,20.25

I try test the others answers. Thanks again

ACA
 
G

Gunnar Hjalmarsson

Mirco said:
Thus spoke Gunnar Hjalmarsson (on 2006-05-29 12:53):
s/(\.\d*[1-9])0+\b|\.0*\b/$1 or ''/eg;

$text = "20.01";
$text =~s/(\.\d*[1-9])0+\b|\.0*\b/$1 or ''/eg;
print "$text\n";

==> 2001

Okay, new attempt:

s/(\.\d*[1-9])0+\b|\.0*(?!\d)/$1 or ''/eg;

:)
 
D

Dr.Ruud

aca schreef:
I dont know how I can replace the zeros on the right of the decimal
separator, for example I have a file with some lines of data how this
example:

20.1300,20.2500,19.7700,20.2500,985365,0

And I want that the final text appear how this

20.13,20.25,19.77,20.25,985365,0

I try use the regular expression but the final patron I dont know how
it can be variable.

$ echo "10.00,20.1300,20.2500,19.7700,20.2500,985365,0.010" | perl -pe '
s/\b(\d+[.]\d*?)0+\b/$1/g, s/[.](\D)/$1/g
'

10,20.13,20.25,19.77,20.25,985365,0.01
 
M

Mirco Wahab

Thus spoke Gunnar Hjalmarsson (on 2006-05-29 13:26):
Okay, new attempt:

s/(\.\d*[1-9])0+\b|\.0*(?!\d)/$1 or ''/eg;

my $text = qq{2000,200.0000,200.,20.010000,.0202,985365,0};
$text=~ s/(\.\d*[1-9])0+\b|\.0*(?!\d)/$1 or ''/eg;
print $text;


=> 2000,200,200,20.01,.0202,985365,0

Looks good!

Regards

Mirco
 
X

Xicheng Jia

aca said:
Hi, from Spain, I'm new user in perl, I have a problem with regular
expression,

I dont know how I can replace the zeros on the right of the decimal
separator, for example I have a file with some lines of data how this
example:

20.1300,20.2500,19.7700,20.2500,985365,0

And I want that the final text appear how this

20.13,20.25,19.77,20.25,985365,0

I try use the regular expression but the final patron I dont know how
it can
be variable.

$string =~ s/(\.\d*?)0+(?=,|$)/$1/g;

Xicheng
 
A

aca

Thanks Gunnar for your time but the result are the same, appear the
data but without decimals
20,20,19,20 and not 20.13,20.25,19.77,20.25

I will try more slowly the solution of split mentioned more above by
mirco,
but for me is the solution more hard.

Thanks to all, I hope that some day in the future I will can help to
others, but
for at present, I only can study.

ACA
 
M

Mirco Wahab

Thus spoke Xicheng Jia (on 2006-05-29 13:36):
$string =~ s/(\.\d*?)0+(?=,|$)/$1/g;

$text = "200.,200.0000";
$text =~ s/(\.\d*?)0+(?=,|$)/$1/g;
print $text ;

==> 200.,200.

Regards

Mirco
 
L

Lars Haugseth

|
| The first thanks to all for yours rapid answers.
|
| Lars I try you solution , because it the first that I understand, but
| the final results
| are :
|
| 20,20,19,20 and not 20.13,20.25,19.77,20.25

Are you sure? It is working as intended here (perl 5.8.7):

$ echo "20.1300,20.2500,19.7700,20.2500,985365,0" |
perl -pne 's/(\.\d+?)0+(?!\d)/$1/g;'
20.13,20.25,19.77,20.25,985365,0
 
G

Gunnar Hjalmarsson

aca said:
Gunnar said:
s/(\.\d*[1-9])0+\b|\.0*(?!\d)/$1 or ''/eg;

Thanks Gunnar for your time but the result are the same, appear the
data but without decimals
20,20,19,20 and not 20.13,20.25,19.77,20.25

I have a feeling that you don't use the s/// operator properly. Show us
the code that gives you that result, and we can help you correct it.

Also, to take full advantage of this Usenet group, please read and
follow the posting guidelines.
http://groups.google.com/group/comp.lang.perl.misc/msg/b1b69d3cbfeaa440
 
M

Mirco Wahab

Thus spoke aca (on 2006-05-29 12:06):
Hi, from Spain, I'm new user in perl, I have a problem with regular
expression,

I dont know how I can replace the zeros on the right of the decimal
separator, for example I have a file with some lines of data how this
example:

20.1300,20.2500,19.7700,20.2500,985365,0

After criticizing others here, I should
show up also with some solution ;-)
(to get some bashes):

...
my $text = qq{2000,200.0,20.01,.020200,985365,0};
$text = join ',', map{sprintf "%g",$_} split /,/, $text;
...

=> 2000,200,20.01,0.0202,985365,0

Regards

Mirco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top