in reply to Searching for files efficiently help!

Definitely what jethro says.

A minor note - your $files regex stores two values but you only use one. And if you change the delimiter, you don't have to escape the slash. So, instead of:

if($files=~/(.*?)\/([^\/]+)$/) { $f_name = $2; }

Either:

# match all non-slashes up to end of line if($files=~m|([^/]+)$|) { $f_name = $1; }

Or:

# match everything beyond last slash if($files=~m|.*/(.+)|) { $f_name = $1; }

Replies are listed 'Best First'.
Re^2: Searching for files efficiently help!
by Anonymous Monk on Nov 16, 2011 at 19:02 UTC
    I have a question for you, if the values in this array @filelist was like this:my @filelist = qw(88732 99877 76211); I would have to match if these values are part of these file names in array @files, how would the regular expression be in this case. In another words matching values from @filelist against values in in the array @files that would be like this: my @files = qw(99877_bc_20101000.txt 99877_xy_20111111.txt 76211_bc_20101000.txt);
    I hope I was clear on that. Thanks!

      If I understand, you can simply narrow your regex to match up to the first underscore, and see if that 'exists':

      foreach my $files(@files) { $c++; if($files=~m|.*/([^_]+)|) { $f_name = $1; } if (exists $filelist{$f_name}) { print "$c - Found and to be deleted: $files\n"; } }

      I might go as far as:

      for (@files) { $c++; if(m|.*/([^_]+)| && exists $filelist{$1} ) { print "$c - Found and to be deleted: $_\n"; } }
        I like this, thanks very much!
      In this case I would put back the second foreach loop:
      foreach my $chk_file(@filelist) { #if($chk_file=~/$f_name/) { if($f_name=~/$chk_file/) { print "$c - Found and to be deleted: $files\n"; } }