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

Hi there.

Need some urgent help here. I'm trying to verify if a file that is in an array exists or not.

Well, I usually seek for just one file so I use the good old:

unless (-e $file)

But now I must verify if at least one of the files of an array exists. I've tried the following, that some monks posted on the chatterbox:

grep (-e $_) @files;
or
unless grep /1/, (map -e $_, @files)

But both gives me syntaxes errors, so PLEASE if anybody can help me...

I really need this.

Thanks in advance,

Er Galvão Abbott
a.k.a. Lobo, DaWolf
Webdeveloper

Replies are listed 'Best First'.
Re: Seeking multiple defined files
by Fastolfe (Vicar) on Nov 21, 2000 at 21:07 UTC
    You're using (parenthesis) when you should be using {curly-braces}. The first argument to grep is a { BLOCK } that describes how you want to search the items in the following list.
    @existing_files = grep { -e $_ } @untested_files;
    This will return true for any item that can be said to "exist" in the file system. You may want to use -f to test if it's a file (versus, say, -d to see if it's a directory), etc. See _X for additional tests you can perform. Note also that $_ is implied, so you can leave that out: { -e }

    # alternate syntax @exiting = grep(-e, @untested);
    See the documentation for grep, which is what I would have done in the first place.
Re: Seeking multiple defined files
by mikfire (Deacon) on Nov 21, 2000 at 21:13 UTC
    merlyns's comment is on the button. Becuase I am in hard core work avoidance mode, though, I will offer some suggestions

    Most of these snippets will assume the list of filenames is in ( imagine this ) @filenames. I will state other assumptions later

    If you don't care to preserve the filenames, this one is kinda neat. It is compact and has the convenient side effect of giving you the name of the file that first matched.

    shift @filenames while ( @filenames && ! -e $filenames[0] ); die "No names matched" unless ( @filenames );

    If you must preserve the data, you will likely want a foreach loop. This loop will also preserve any match. It is also a bit more verbose than the previous example.

    my $found = ''; foreach ( @filenames ) { next unless -e $_; $found = $_; last; }
    These will give you better average case performance than either of the two methods you mentioned in your question. The grep-map combo is really nasty - it will walk through the entire list twice even if the first file in the list exists.

    Mik mikfire

Re: Seeking multiple defined files
by chromatic (Archbishop) on Nov 21, 2000 at 21:01 UTC
    The first of the two chatterbox solutions (mine, actually :) works fine on my machine. What is the syntax error you are receiving?

    (add 'use diagnostics;' to the start of your program and see if that helps your debugging.)

      Hi Chromatic.

      I've tried the command you've putted:

      grep (-e $_) @files;

      I receive these error messages:

      Array found where operator expected at foo.cgi line 187 at end of line. (Missing operator before ?)

      Not enough arguments for grep at foo.cgi line 187 near "$_)"

      Syntax error at foo.cgi line 187, near ") @files"

      Er Galvão Abbott
      a.k.a. Lobo, DaWolf
      Webdeveloper
Re: Seeking multiple defined files
by merlyn (Sage) on Nov 21, 2000 at 20:55 UTC