I have a bunch of strings like this:
my $string1 = "xg0000";
my $string2 = "fx0015";
What do I do to increase the number in the string by 1 but also maintain the leading zeros to keep the length of the string the same.
I tried this:
$string =~ s/(\d+)/0 x length(int($1)) . ($1+1)/e;
It doesn’t seem to work on all numbers. Is regex what I’m supposet to use to do this or is there a better way?
How about a little perl magic? The
++operator will work even on strings, and0000will magically turn into0001.Now, we can’t modify
$1since it is readonly, but we can use an intermediate variable.Update:
I didn’t think of this before, but it actually works without a regex:
Still does not solve the problem DavidO pointed out, with
9999. You would have to decide what to do with those numbers. Perl has a rather interesting solution for it: