I've commented out two lines in your code and replaced them with mine.

I think it should work, but let us know if it doesn't.

Good Luck!

Joshua Colson
Systems Administrator
Giant Industries, Inc.
(480) 585-8714
[EMAIL PROTECTED]


-----Original Message-----
From: John Edwards [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 17, 2002 10:43 AM
To: Perl Beginners (E-mail)
Subject: Regex Assistance


Hi group. 

I have the following snippet of code. It's not working and I've been going
round in circles trying to figure out why.

I need a routine that will look at the filename, if that filename already
exists, then add a (1) to the end. I've got the checking for existance
sorted, it's the generation of the new file name that is the issue.

E.g. test.txt exists so create test(1).txt

If test(1).txt exists then create test(2).txt and so on

Here is the problematic code I have so far

-- code --
my ($name, $ext) = split(/\./,$fileoutname);

    #if ($name =~ /\((\d{1,1})\)$/) { # Looks for (1) on the end for example
    if ($name =~ /\((\d)\)$/) { # the \d assumes one character. {1,1} is
just to specify range.
        my $number = $1;
        $number++;
        # $name =~ tr/\(\d\)/\($number\)/;
        $name =~ s/\(\d\)$/\($number\)/;        # this line should do the
trick.
    } else {
        $name .= "(1)";
}

$fileoutname = "$name\.$ext";

-- end code --

There's probally some really basic errors in there, and maybe a much better
way of doing it...

TIA
                                
John


Reply via email to