Solved

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

Posted on 2003-11-19
14
258 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now