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

Hey guys, this should be an easy one for someone wiser than I. I'm using File::Find to search through a directory and grab files with .txt, .jpg, and .gif files. I then want to put them into an array. However, I don't want to grab any files named "category.txt". For some reason my logic below isn't working and the category.txt files are being included. Can someone point me in the right direction?
find(\&wanted, $dir); my @file_list; sub wanted{ next if (-l && !-e ); push (@file_list, $File::Find::name) if ((($_ =~ m/.txt$/gi) && ($ +_ !~ m/category.txt$/gi)) || ($_ =~ m/.jpg$/gi) || ($_=~ m/.tif$/gi)); }

Replies are listed 'Best First'.
Re: Push, Matching, and File::Find
by ikegami (Patriarch) on Feb 17, 2010 at 19:22 UTC

    Not only does if (//g) doesn't make sense conceptually (search again just to be sure?), it's buggy to do so*. You also seem to be forgetting that "." means something special in a regex pattern. Fix:

    push(@file_list, $File::Find::name) if /\.(?:txt|jpg|gif)\z/i && lc($_) ne 'category.txt';

    * — while (//g) and if (//gc) are useful, but that's another story.

      Not trying to nitpick, but Op used plural for "category.txt files", so in case of housing_category.txt or job_category.txt, I guess we could have a slight change?:
      push(@file_list, $File::Find::name) if /\.(?:txt|jpg|gif)\z/i && !/category\.txt\z/i;
      Update: well I wasn't exactly sure whether there could be more than one category.txt type file out there. Anyway OP has some solid code (And I really do like the ikegami code). I guess since I'm commenting more, I would point out to the op that "return" looks like what was intended rather than "next" at top of sub.

        He said:

        I don't want to grab any files named "category.txt"

        The plural indicate similarly named files could exist in multiple directories.