Bintuch has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks,

How can I reverse only the numbers in a string?

For example: abcdef12345ghijklm6789nop

Thanks.

Replies are listed 'Best First'.
Re: Reverse fraction of a string
by Corion (Patriarch) on Sep 19, 2010 at 21:12 UTC

    Most likely your course material contains the information relevant to solving this task.

    How would you go about reversing only the numbers in a string, if you had to do it manually, without a computer?

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Reverse fraction of a string
by perlpie (Beadle) on Sep 19, 2010 at 22:12 UTC

    Here's a solution for one interpretation of your question:

    #!/usr/bin/perl use strict; use warnings; (my $string = shift()) =~ s/(\d+)/reverse $1/ge; print "$string\n";

    Save as foo.pl and then call it as

    foo.pl abc123def4567g890

    to get output like

    abc321def7654g098
Re: Reverse fraction of a string
by GrandFather (Saint) on Sep 19, 2010 at 22:21 UTC

    You seem to have been around here for a few years so it's less likely that this is a homework question. If that is the case then perhaps you need to tell us a little more about what you actually need to do? We can generally provide better answers if you give us more information, and especially if you show us what you have tried already. But then, you should already know that.

    You may find that reverse is a key to solving your problem however.

    True laziness is hard work

        Guys, it is not homework...

        I'm not using Perl extensively and therefore I'm not aware of all the options. All I wanted is a little help... No need to make a fuss.

        perlpie, many thanks for your answer. I did not know the '/e' operator.

Re: Reverse fraction of a string
by murugu (Curate) on Sep 20, 2010 at 05:52 UTC
    $string = 'abcdef12345ghijklm6789nop'; $string=~s/(\d+)/reverse $1/ge;

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

      Thanks!
A reply falls below the community's threshold of quality. You may see it by logging in.