• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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".
0
mikezang
Asked:
mikezang
  • 7
  • 6
1 Solution
 
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now