in reply to Re: Increment a string with letters and numbers
in thread Increment a string with letters and numbers

upon further examination, already submitted. ah well.
  • Comment on Re^2: Increment a string with letters and numbers

Replies are listed 'Best First'.
Re^3: Increment a string with letters and numbers
by emptyshell (Initiate) on May 21, 2013 at 02:24 UTC

    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;

      Using the solution given above:

      13:09 >perl -wE "my $s = 'EVO:001 xyz345 EVO:456'; $s =~ s/(EVO:)(\d+) +/$1.++($_=$2)/eg; say $s;" EVO:002 xyz345 EVO:457 13:13 >

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        YES!!!!!!!!!!!!!!!!!!!!!!! Athanasius you are my hero for at least a week. Thank you very much and have a great day!