in reply to Seeking multiple defined files
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.
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.my $found = ''; foreach ( @filenames ) { next unless -e $_; $found = $_; last; }
Mik mikfire
|
|---|