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

Regex Assistance

Thread Next
John Edwards
January 17, 2002 09:47
Regex Assistance
Message ID:
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;
        $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...


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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About