How to inc or dec a string like "j000012.html"

I have a simple question, I want to inc or dec string, for example, "j000012.html" to "j000011.html" or "j000013.html".
LVL 5
mikezangAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
Depends what language you are talking about.  What's your preference?


0
mikezangAuthor Commented:
Sorry, that is Perl.
0
ozoCommented:
$string = "j000012.html";
substr($string,1,6)++;
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

ozoCommented:
$string =~ s/(\d+)/my $i=$1;++$i/e;
0
ozoCommented:
Decrement is a little trickier
(what would "j000000.html"  be when decremented?)

string = "j000012.html";
for( substr$string,1,6 ){
     tr/0-9/9876543210;
    $_++;
    tr/0-9/9876543210/;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikezangAuthor Commented:
Thanks a lot!
0
mikezangAuthor Commented:
Hi
Itried $string =~ s/(\d+)/my $i=$1;++$i/e;
it's ok, but when I tried $string =~ s/(\d+)/my $i=$1;--$i/e;
I only got "j11.html", not "j000011.html", can you help me again?
0
mikezangAuthor Commented:
Then if $string = "gs19/j000012.html", it also doesn't qork.
Can you help me?
0
ozoCommented:
As I said, decrement is a little trickyer.
You could try something like

$string =~ s/(\d+)(\.html)/my $i=$1;$i=~tr!0-9!9876543210!;++$i;$i=~tr!9876543210!0-9!;$i.$2/e;

or did you want to decrement both the 19 and the 000012?
0
mikezangAuthor Commented:
No, I only want to inc or dec filename, not directory.
0
ozoCommented:
what identifies the part you want to inc or dec?
0
mikezangAuthor Commented:
$file = "gs19/j000003.html" or "qjs/j00003.html" or "....../xxx0003.html"

I hope
$prev = "gs19/j000002.html" or "qjs/j00002.html" or "....../xxx0002.html"
$next = "gs19/j000004.html" or "qjs/j00004.html" or "....../xxx0004.html"

0
ozoCommented:
If it's always 6 digits .html then
($next = $file) =~ s/(\d{6})(\.html)/sprintf"%06d$2",$1+1/ie;
($prev = $file) =~ s/(\d{6})(\.html)/sprintf"%06d$2",$1-1/ie;
0
mikezangAuthor Commented:
Thanks a lot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.