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

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;

Replies are listed 'Best First'.
Re^4: Increment a string with letters and numbers
by Athanasius (Archbishop) on May 21, 2013 at 03:14 UTC

    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!