Solved

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

Posted on 2003-11-19
14
266 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Facebook React and SyntheticEvents 6 76
pass a variable from a list to a batch file using psexec 11 74
Logon script fails 23 61
Review of the nodejs code 1 36
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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)

749 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