help with Globbing : use of -r with glob catches only half of the matching files ...

X

xhoster

martin said:
Hi, I am trying to use a combination of file test with '-r' and file
globbing to check existense of a certain number of files on a linux
system. I appreciate any help on this, as I catch only half of the
files that are supposed to be detected.

from perldoc perlop:

A (file)glob evaluates its (embedded) argument only when it is
starting a new list. All values must be read before it will start
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
over. In list context, this isn't important because you
^^^^
automatically get them all anyway. However, in scalar context the
operator returns the next value each time it's called, or "undef"
when the list has run out. As with filehandle reads, an automatic
"defined" is generated when the glob occurs in the test part of a
"while", because legal glob returns (e.g. a file called 0) would
otherwise terminate the loop. Again, "undef" is returned only once.
So if you're expecting a single value from a glob, it is much better
to say

($file) = <blurch*>;

than

$file = <blurch*>;


loop here ---> while (<FILEwithlistofFiles>) {

Perl already has a system for comments. Please use it
rather than making up your own:

my $file = $_;

if (-r ($globFile = glob("$file*.ex"))) {

my ($globFile)= glob("$file*.ex"); # call in list context
if (defined $globFile and -r $globFile) {

Cheers,

Xho
 
M

martin

Hi, I am trying to use a combination of file test with '-r' and file
globbing to check existense of a certain number of files on a linux
system. I appreciate any help on this, as I catch only half of the
files that are supposed to be detected.

I open a file that has a list of files with format of each line:

/dir1/subdir2/subdir3/file.xy <-- full path of a sample file

for any file "file.xy" there are several files in the system
such as

file.xy.0
file.xy.1
file.xy.1.2
file.xy.1.2.3
and one file which has the .ex at the end
file.ext.xy.1.2.4.ex


the part between .xy and .ex could be any number of times occurding
..[\d]*

My goal is to read list of files one by one and then to see if each
*.xy file has a corresponding *.xy....ex file.


I use this peace of code but out of 100 files, it only catches the
alternate files. and misses the other ones.

use strict;
use warnings;

my $globFile;

loop here ---> while (<FILEwithlistofFiles>) {
my $file = $_;

if (-r ($globFile = glob("$file*.ex"))) {
print " Found a matching file with .ex extension at the end.
print "found file is $globefile \n";
}
else {
print "missed to find the matching file with ending .ex extension in
the directory tree\n";
}

I should add that all the files are visible from the directory where I
invoke the script.

any help appreciated.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top