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

How can I get the basename of this array in to another array neatly ?

my @result = `find '.' -type f -print -name '*.pl'` or die "No scripts found - $! \n"

Thanks

Replies are listed 'Best First'.
Re: Using Basename
by broquaint (Abbot) on Nov 17, 2003 at 15:53 UTC
    Just use map with File::Basename and make sure you drop the trailing newlines
    use File::Basename; my @basenames = map basename($_), my @result = map { chomp; $_ } `find '.' -type f -print -name '*.pl'` or die "No scripts found - $! \n";
    Or more perl-ishly
    use File::Basename; use File::Find::Rule; my @basenames = map basename($_), my @result = find(file => name => "*.pl", in => ".") or die "No scripts found - $! \n";
    See. map, File::Basename and File::Find::Rule for more info.
    HTH

    _________
    broquaint

      thanks for that. I tried first option but having problems picking up correct files and printing. doesn't seem to recognise *.pl and I'm wondering whether using the current directory is causing a problem. First return off print statement looked like the opening screen of the matrix
        blueapache,
        broquaint said HTH (had to hurry). Try this slightly modified code:
        #!/usr/bin/perl -w use strict; use File::Basename; my @result = `find '.' -type f -name '*.pl' -print` or die "No scripts + found - $! \n"; chomp @result; my @basenames = map { basename($_) } @result; print $_, $/ for @basenames;
        Cheers - L~R
Re: Using Basename
by TomDLux (Vicar) on Nov 17, 2003 at 20:01 UTC

    The clauses of a find are carried out in order, left to right. If a clause succeeds, processing continues; if it fails, this candidate is abandoned and the next file tested.

    • In the case above, -type f tests whether the current candidate is a file, or directory or link or something else. Processing continues only if it is a file.
    • Next, the name of the file is printed. Printing always considered to succeed.
    • Finally, the name is tested to see if matches the glb pattern, '*.pl'. Whether it matches or not, processing is finished for this candidate.

    Presumably, what you really want is to defer printing until after you have selected only '*.pl' files:

    .... `find '.' -type f -name '*.pl' -print` ....

    info find or man find is your friend. In particular, watch for the case where you want to match one of two patterns, using -o. You have to group the patterns using parentheses, escaped to prevent the shell from interpreting them:

    find . \( -name '.pl' -o -name '*.pod' \) -print

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      you must have read my mind, used broquaint's code which works great but I was trying to do what your code does, just one step at a time