?
Solved

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

Posted on 2003-11-19
14
Medium Priority
?
274 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
[X]
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
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 84

Expert Comment

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

Accepted Solution

by:
ozo earned 200 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

801 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