Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Files and -f

by mndoci (Scribe)
on Dec 01, 2001 at 00:53 UTC ( [id://128739]=perlquestion: print w/replies, xml ) Need Help??

mndoci has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks I am reading a list of names from a file in the format

abc
def
geh

and so on the names are the prefix to a bunch of files which go like

abc.txt
def1.txt
geh.txt

What I want to do is go through the original list line by line and open the corresponding txt file. Easy enough. I wrote something like
chomp $_; # where $_ is the current line from the original list my $prefix = $_; my $txtfile = $prefix . ".txt"; if (-f $txtfile){ do stuff } else{ $txtfile1 = $prefix . "1.txt"; # the # is always 1 do stuff }
This is not working as in the else statement the "do stuff" includes an open statement and , e.g., the script tries to open abc1.txt which of course does not exist. In other words if I find a file I want to open it, perform some other actions and move on and so on. I am trying a  if (!(-f $file)) type syntax as I write this, but does anyone have a better idea.

Thanks
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'

Replies are listed 'Best First'.
Re: Files and -f
by Purdy (Hermit) on Dec 01, 2001 at 01:06 UTC
    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

      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'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://128739]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-26 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found