in reply to incrementing strings
In such cases, one is tempted to write this:
That's however invalid, so let's do this:$string = "johnq.smith1"; $string =~ /(\d+$)/ or die "no number"; $1++ +; print $string, $/; # WRONG
that doesn't have much advantage over a simple substitution of course.$string = "johnq.smith1"; $string =~ /(\d+$)/ or die "no number"; matc +h($string, 1)++; print $string, $/; sub match : lvalue { my $n = $_[1] || 0; substr $_[0], $-[$n], $+[$n] +- $-[$n]; }
One could even create a tied array for the modifiable versions of $1, $2, ...
{ package Matched; sub TIEARRAY { bless \my$x, $_[0]; } sub FETCH { my + $n = $_[1]; substr $_, $-[$n], $+[$n] - $-[$n]; } sub STORE { my $n += $_[1]; substr($_, $-[$n], $+[$n] - $-[$n]) = $_[2]; } tie @~, Match +ed::; } local $\ = $/; $_ = "johnq.smith1"; /(\d+$)/ or die "no number"; $~[1] +++; print;
|
|---|