in reply to Files and -f

Methinks your logic is a little off... if I understand you correctly, you want to test to see if the file exists (with either a '1' or not in the filename. And if so, open it and do whatever. Otherwise, move on (or give an alert or something). The '-f' expression tests to see if the file is a regular file (on top of it existing), but that's not really what's hanging you up, I bet.

my $txtfile = $prefix . ".txt"; my $txtfile1 = $prefix . "1.txt"; if ( -f $txtfile ) { # do stuff } elsif ( -f $txtfile1 ) { # do stuff } else { # no file was found }

You may want to even share whatever 'stuff' you have to do:

if ( -f $txtfile || -f $txtfile1 ) { if ( -f $txtfile ) { open(FILE, "$txtfile"); } else { open(FILE, "$txtfile1"); } # do stuff with FILE close (FILE); } else { # no file was found }

Disclaimer: Code is untested and there are probably better ways to streamline the code - aiming for readability.

Jason

Replies are listed 'Best First'.
Re: Re: Files and -f
by mndoci (Scribe) on Dec 01, 2001 at 01:53 UTC
    Duh!!, Me that is. You are, of course, quite right.
    Thanks for the tip.

    mndoci

    "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'