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

Hi, I need a monks help on this one
for (`find /data/ -name "*.IND" | grep -E "xxx|yyy"`) ($patch) = ($_ =~ /.*\/(.*)\..*$/); @patch = $patch shift || '.';
it is somthing like that, but im not sure how i should do it so if anyione have any idea.. i would like to hear aboute them Thanks, Miller

Replies are listed 'Best First'.
Re: a find question !!!!
by grinder (Bishop) on Aug 06, 2001 at 14:17 UTC
    I sort of understand the question having seen it thrashed out in the CB over the last few days, but as it stands your question isn't easy to parse.

    IIRC, you are looking for files that have a .ind extension (to use Win32 terminology), that also contain xxx or yyy in the pathname. My advice in the CB was to use File::Find... and this is how I'd do it:

    use strict; use File::Find; my @patch; find( sub { next if $File::Find::name =~ /xxx|yyy/; # somewhere in the full na +me next unless /\.ind$/i; # just considering the file itself push @patch, $File::Find::name; }, shift || '.' # starting directory from command line, or current dir );

    That said, I have a particular aversion to building up a potentially huge array, if the only thing I am going to do afterwards is for( @patch ) { do_something() }. Rather than pushing it onto an array, I would just deal with it on the spot. Tread lightly on your machine, and it will serve you well.

    Also note that $File::Find::name is the canonical name of the file e.g. /tmp/foo/bar/thing.ind, whereas $_ (in the context of find's callback sub) is merely thing.ind.

    update: Duh! There was one other thing I meant to say. If 'xxx' or 'yyy' appears the name of a directory at the top of a very deep tree, the script will spend a lot of useless time delving into that tree for no purpose, since the first test will keep returning false. This calls for judicious use of the $File::Find::Prune variable, but that is either beyond the scope of this reply and/or left as an exercise to the reader!

    --
    g r i n d e r
Re: a find question !!!!
by davorg (Chancellor) on Aug 06, 2001 at 13:40 UTC

    I think that you should probably explain what you're trying to do rather than just post code as it's not really clear what your code is trying to do.

    You should probably be looking at the File::Find module as it allows you to implement find-style things without using an external shell.

    --
    <http://www.dave.org.uk>

    Perl Training in the UK <http://www.iterative-software.com>

A find question !!!!
by miller (Acolyte) on Aug 06, 2001 at 14:22 UTC
    ok, iŽll try to be a little more clear i have a dir "/data" with all the patches for a program, but in that dir is also patches for other program. the patches all make there own directories so the patches are in "/data/$patch" and in order for me to get the patch name then i have to do an regex. and last i need to put all the patches into a array
    for (`find /data/ -name "*.IND" | grep -E "xxx|yyy"`) ($patch) = ($_ =~ /.*\/(.*)\..*$/); @patch = $patch shift || '.';
    I hope that this was more helpful :-) Thanks, Miller

      Here is a another way to get them:

      my @patches = grep {/xxx|yyy/} </data/*/*.IND*>; # Above gives the full path to the patches # If you just want the filename, not the full path, use this: #my @patches = map { (split /\//)[-1] } grep {/xxx|yyy/} </data/*/*.I +ND*>;

      Perl grep filters a list down to those elements which leave its first argument true. The diamond operator with glob argument takes care of the file finding.

      After Compline,
      Zaxo