in reply to Specific instance of a repeated string

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.

Replies are listed 'Best First'.
Re^2: Specific instance of a repeated string
by Ionizor (Pilgrim) on Aug 13, 2003 at 02:17 UTC

    I guess I wasn't clear. The file suffix is the part of the file after the number I'm operating on (for the file foo10bar.baz the suffix would be bar.baz. In most cases the suffix is just the file extension but I'm trying to make the script handle any filename regardless of the position of the digits.

    I've gotten some inspiration from your code though, so thanks! I'll let you know how it turns out.

    --
    Grant me the wisdom to shut my mouth when I don't know what I'm talking about.