Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Increment a string with letters and numbers

by kcott (Abbot)
on Mar 28, 2013 at 09:08 UTC ( #1025913=note: print w/ replies, xml ) Need Help??


in reply to Increment a string with letters and numbers

G'day mrguy123,

The following substitution should do what you want:

s/(\d+)$/my $y = $1; ++$y/e

In use with your specific example:

$ perl -Mstrict -Mwarnings -E ' my $x = q{EVO:0000023}; $x =~ s/(\d+)$/my $y = $1; ++$y/e; say $x ' EVO:0000024

And, assuming you want 0000099 to roll over to 0000100, this works unchanged:

$ perl -Mstrict -Mwarnings -E ' my $x = q{EVO:0000099}; $x =~ s/(\d+)$/my $y = $1; ++$y/e; say $x ' EVO:0000100

-- Ken


Comment on Re: Increment a string with letters and numbers
Select or Download Code
Replies are listed 'Best First'.
Re^2: Increment a string with letters and numbers
by NetWallah (Abbot) on Mar 28, 2013 at 19:12 UTC
    Slightly shorter version of Ken's idea, taking advantage of auto-localization of $_:
    >perl -E "$_=shift @ARGV; say qq|Before: $_|; s/(\d+)/++($_=$1)/e; say + qq|after : $_|" EVO:00000000022 Before: EVO:00000000022 after : EVO:00000000023

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025913]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2015-07-31 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls