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

Hi All,
Having a bit of trouble with a grep. I have a directory containing 3 items: file1, file2 and file3. The following code is supposed to exclude file1 and file2 but when I print the array only the first one gets excluded. Any ideas? Thanks

my @files2 = map { lc } grep { -f "$dir2/$_" } grep { $_ ne "file1" } grep { $_ ne "file2" }readdir(DIR2);
print "@files2\n";

prints: file2 file3

Replies are listed 'Best First'.
Re: Grep Array Multiple times
by toolic (Bishop) on Sep 23, 2010 at 16:33 UTC
    Maybe readdir is not returning what you expect it to return. Try to debug a smaller piece of it:
    use Data::Dumper; my @files2 = readdir(DIR2); print Dumper(\@files2);

    When I remove the readdir, I get what you expect:

    use strict; use warnings; use Data::Dumper; my @files2 = grep { $_ ne "file1" } grep { $_ ne "file2" } qw(file1 fi +le2 file3); print Dumper(\@files2); __END__ $VAR1 = [ 'file3' ];

    See also Basic debugging checklist

      It seems to work after I move map { lc } to after the greps. Thanks!
        A small point, but perhaps worth mentioning... You've got 3 grep's and a map. Each one of these things essentially does a foreach on its input. With Perl grep you can put anything that you want to into the grep - whatever that is, it only has to return a true or a false value. If true the input element from the right passes to the left. So all 3 grep's could be combined into one as shown below. Does the same thing but will run a bit faster - although usually of no importance.

        It is also possible to combine all this into a single map although I don't think that I'd do it in this case because it reads to me as less clear...this is just an example of "how" should the desire arise... The important part here is the return value. Returning () from a map is different than returning undef. Undef is a value. () means "nothing is returned".

        I guess as a side point, I like lining up logical conditions because human beings are "programmed" by instinct to process information better from top to bottom better than left to right and certainly better than diagonally. So when things don't fit nicely on one printed line, I do it like below.

        my @files2 = grep { $_ ne "file1" and $_ ne "file2" and -f "$dir2/$_" }map { lc }readdir(DIR2); my @files = map { lc; ( $_ ne "file1" and $_ ne "file2" and -f "$dir2/$_" ) ? $_ :() }readdir(DIR2);