develooper Front page | perl.beginners | Postings from January 2002

Regex Assistance

Thread Next
From:
John Edwards
Date:
January 17, 2002 09:47
Subject:
Regex Assistance
Message ID:
2FB59B145095D511A7C90050BAC349F31730A3@MAIL
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
        my $number = $1;
        $number++;
        $name =~ tr/\(\d\)/\($number\)/;
    } 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


--------------------------Confidentiality--------------------------.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About