Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Increment a string with letters and numbers

by mrguy123 (Hermit)
on Mar 28, 2013 at 08:44 UTC ( #1025902=perlquestion: print w/ replies, xml ) Need Help??
mrguy123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a string that looks like this:
EVO:0000023
I want to add one to the number - making this into
EVO:0000024
Now I know I can parse the digits in the string, add one and format it with sprintf to keep the zeroes. Then I can concatenate the letter part and it will work.
However, since this is Perl, there has to be a quicker and more elegant way to do this.
And ideas?
Mr. Guy

'You earnest Sage!' aloud they cried, 'your book you've read enough in!
'We wish to chop you into bits to mix you into Stuffin!'


Update: received some pretty cool and elegant answers. Thanks!!

Comment on Increment a string with letters and numbers
Select or Download Code
Re: Increment a string with letters and numbers
by Anonymous Monk on Mar 28, 2013 at 08:45 UTC

    However, since this is Perl, there has to be a quicker and more elegant way to do this.

    Nope

Re: Increment a string with letters and numbers
by jwkrahn (Monsignor) on Mar 28, 2013 at 08:56 UTC
    $ perl -le'$_ = "EVO:0000023"; print; substr( $_, -7 )++; print' EVO:0000023 EVO:0000024
      Nicely done :)
      Thanks!
Re: Increment a string with letters and numbers
by vinoth.ree (Parson) on Mar 28, 2013 at 08:59 UTC
Re: Increment a string with letters and numbers
by hdb (Parson) on Mar 28, 2013 at 09:00 UTC

    More or less same procedure as yours but in one expression. Probably not much of an improvement...

    while(<DATA>) { s/(\D+)(0*)(\d+)/sprintf("%s%07d",$1,$3+1)/e; print; } __DATA__ EVO:0000023 EVO:0000099 EVO:0010099 EVO:9999999
Re: Increment a string with letters and numbers
by kcott (Abbot) on Mar 28, 2013 at 09:08 UTC

    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

      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

Re: Increment a string with letters and numbers
by LanX (Canon) on Mar 28, 2013 at 20:15 UTC
    hmm no one offered split till now :)

    DB<107> ($str,$n)=split /:/,'EVO:0000023' => ("EVO", "0000023") DB<108> print "$str:",++$n EVO:0000024 DB<109> print "$str:",++$n EVO:0000025

    ++ is magic and does a string increment as long as you don't use $n in numeric context otherwise use sprintf

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: Increment a string with letters and numbers
by kschwab (Priest) on Mar 28, 2013 at 21:09 UTC
    echo "EVO:0000023" | perl -pe 's/(\d+)/++($_=$1)/e;'
      upon further examination, already submitted. ah well.

        I have the same question but how is this done if there are for example EVO:001, EV0:002, EVO:003 in a file and I want each one incremented.. I just want to supply EVO: then have it increment the number part. The closest I have gotten is this but it injects a one instead of incrementing the variable

        #!perl=C:\bin\perl use warnings; use strict; use Tie::File; tie my @data, 'Tie::File', 'myfile.txt' or die $!; sub inc { my ($num) = @_; ++$num } s/(MyString\d+)/$1 . (inc($2))/eg for @data; untie @data;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (15)
As of 2014-08-01 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (21 votes), past polls