replace string with variable

Discussion in 'Perl Misc' started by aca, May 29, 2006.

  1. aca

    aca Guest

    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
    aca, May 29, 2006
    #1
    1. Advertising

  2. aca

    Mirco Wahab Guest

    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
    Mirco Wahab, May 29, 2006
    #2
    1. Advertising

  3. * "aca" <> 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
    Lars Haugseth, May 29, 2006
    #3
  4. 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
    Gunnar Hjalmarsson, May 29, 2006
    #4
  5. aca

    Guest

    aca <> 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
    , May 29, 2006
    #5
  6. aca

    Mirco Wahab Guest

    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
    Mirco Wahab, May 29, 2006
    #6
  7. aca

    Mirco Wahab Guest

    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
    Mirco Wahab, May 29, 2006
    #7
  8. aca

    Mirco Wahab Guest

    Thus spoke (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
    Mirco Wahab, May 29, 2006
    #8
  9. aca

    cmic Guest

    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
    cmic, May 29, 2006
    #9
  10. aca

    Mirco Wahab Guest

    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
    Mirco Wahab, May 29, 2006
    #10
  11. aca

    aca Guest

    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
    aca, May 29, 2006
    #11
  12. Mirco Wahab wrote:
    > 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;

    :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 29, 2006
    #12
  13. aca

    Dr.Ruud Guest

    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

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, May 29, 2006
    #13
  14. aca

    Mirco Wahab Guest

    Doooh! Nice one (was: replace string with variable)

    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
    Mirco Wahab, May 29, 2006
    #14
  15. aca

    Xicheng Jia Guest

    aca 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
    >
    > 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
    Xicheng Jia, May 29, 2006
    #15
  16. aca

    aca Guest

    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
    aca, May 29, 2006
    #16
  17. aca

    Mirco Wahab Guest

    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
    Mirco Wahab, May 29, 2006
    #17
  18. * "aca" <> wrote:
    |
    | 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

    --
    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
    Lars Haugseth, May 29, 2006
    #18
  19. aca wrote:
    > Gunnar Hjalmarsson wrote:
    >>
    >> 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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 29, 2006
    #19
  20. aca

    Mirco Wahab Guest

    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
    Mirco Wahab, May 29, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alun
    Replies:
    3
    Views:
    4,507
    Masudur
    Feb 18, 2008
  2. Prasad S
    Replies:
    2
    Views:
    229
    Dr John Stockton
    Aug 27, 2004
  3. Victor
    Replies:
    3
    Views:
    131
    Victor
    Sep 12, 2003
  4. V S Rawat
    Replies:
    5
    Views:
    301
    Richard Cornford
    Jul 3, 2007
  5. X l e c t r i c
    Replies:
    2
    Views:
    97
    X l e c t r i c
    Mar 2, 2008
Loading...

Share This Page