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


In reply to Re: Files and -f by Purdy
in thread Files and -f by mndoci

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.