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

Discussion in 'Perl Misc' started by xhoster@gmail.com, Jun 9, 2006.

  1. Guest

    "martin" <> wrote:
    > 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:

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


    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 9, 2006
    #1
    1. Advertising

  2. martin Guest

    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.
     
    martin, Jun 9, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Georgy Pruss
    Replies:
    15
    Views:
    726
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    362
    Duncan Booth
    Dec 1, 2003
  3. trint
    Replies:
    4
    Views:
    708
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Sep 11, 2007
  4. joe chesak
    Replies:
    7
    Views:
    290
    (r.*n){2}
    Sep 23, 2010
  5. Mike Ballard
    Replies:
    6
    Views:
    274
    Dr.Ruud
    Nov 15, 2005
Loading...

Share This Page