Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2003-11-19
14
265 Views
Last Modified: 2013-12-25
I have a simple question, I want to inc or dec string, for example, "j000012.html" to "j000011.html" or "j000013.html".
0
Comment
Question by:mikezang
  • 7
  • 6
14 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 9783766
Depends what language you are talking about.  What's your preference?


0
 
LVL 5

Author Comment

by:mikezang
ID: 9783837
Sorry, that is Perl.
0
 
LVL 84

Expert Comment

by:ozo
ID: 9785133
$string = "j000012.html";
substr($string,1,6)++;
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 84

Expert Comment

by:ozo
ID: 9785143
$string =~ s/(\d+)/my $i=$1;++$i/e;
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 9785186
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
 
LVL 5

Author Comment

by:mikezang
ID: 9785204
Thanks a lot!
0
 
LVL 5

Author Comment

by:mikezang
ID: 9786822
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
 
LVL 5

Author Comment

by:mikezang
ID: 9788110
Then if $string = "gs19/j000012.html", it also doesn't qork.
Can you help me?
0
 
LVL 84

Expert Comment

by:ozo
ID: 9791965
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
 
LVL 5

Author Comment

by:mikezang
ID: 9792164
No, I only want to inc or dec filename, not directory.
0
 
LVL 84

Expert Comment

by:ozo
ID: 9792380
what identifies the part you want to inc or dec?
0
 
LVL 5

Author Comment

by:mikezang
ID: 9795004
$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
 
LVL 84

Expert Comment

by:ozo
ID: 9795107
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
 
LVL 5

Author Comment

by:mikezang
ID: 9795536
Thanks a lot!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to learn promise API 2 55
Register AutoHotkey 12 85
Logon script fails 23 45
PS call executable Files in Order 4 12
This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question