Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > replace string with variable

Reply
Thread Tools

replace string with variable

 
 
aca
Guest
Posts: n/a
 
      05-29-2006
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

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      05-29-2006
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

 
Reply With Quote
 
 
 
 
Lars Haugseth
Guest
Posts: n/a
 
      05-29-2006

* "aca" <(E-Mail Removed)> wrote:
|
| 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;

--
Lars Haugseth

"If anyone disagrees with anything I say, I am quite prepared not only to
retract it, but also to deny under oath that I ever said it." -Tom Lehrer
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-29-2006
aca wrote:
> 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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      05-29-2006
aca <(E-Mail Removed)> wrote:

> 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


$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

 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      05-29-2006
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
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      05-29-2006
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
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      05-29-2006
Thus spoke http://www.velocityreviews.com/forums/(E-Mail 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
 
Reply With Quote
 
cmic
Guest
Posts: n/a
 
      05-29-2006

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 ...
--
cmic Perl beginner

>
> Regards
>
> Mirco


 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      05-29-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a variable name for the string.replace() regexp X l e c t r i c Javascript 2 03-02-2008 04:31 AM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM
How to replace a variable string within /* variable_string */ with x for each character in string? Victor Perl Misc 3 09-12-2003 04:19 PM



Advertisments