If you want to be able to change any number in the file name, you would probably like to have it split into chunks. So far my idea; I realize that
shenme
has already written a piece of code utilizing this, but TIMTOWDI, and so I decided to write another piece of code:
#!perl -w
use strict;
for my $name (<DATA>) {
chomp $name;
my %parts;
$name =~ s/(.*)(\.[^.]*)/$1/;
$parts{"suffix"} = $2 or die "Not a valid file name: $name";
$parts{"prefix"} = [];
$parts{"number"} = [];
while ($name =~ s/^(\D*)(\d+)//) {
push @{$parts{"prefix"}}, $1;
push @{$parts{"number"}}, $2;
}
$name and die "Invalid file name format. Rest '$name' remained";
print "Filename splits as follows: ["
. join("][",
map { "(" . $parts{"prefix"}->[$_] . ")("
. $parts{"number"}->[$_] . ")" } 0..@{$parts{"nu
+mber"}}-1)
. "]<"
. $parts{"suffix"} . ">\n";
}
__DATA__
01-html02.html
01-htm23-43.htm
01-file-01.html
The program first extracts the file suffix (file ending after the dot, I hope that I didn't misunderstand you here) and then loops through the file name, taking (possibly) a prefix and (definitely) a number from it and storing it in anonymous arrays in
$parts{"prefix"} and
$parts{"number"}.
If you want to increment the
$ith number now, you would just have to write
++$parts{"number"}->[$i];
$filename = join('',
map { $parts{"prefix"}->[$_]
. $parts{"number"}->[$_] }
0..@{$parts{"number"}}-1)
. $parts{"suffix"};
Hope that helped.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.