in reply to Questions on File::Find

I think this is what you're looking for. TIMTOWTDI, of course.

The STRING_TO_ID thing is a little dirty, but is one way of returning clean id's in your results. Also, I'm not sure of the best way to avoid regex characters in the string matches. I guess you could add in \Q and \E in init_matches().

use strict; use warnings; use CGI; use File::Find; use Data::Dumper; use vars qw(@DIRS $ROOT_DIR %ID_TO_STRING %STRING_TO_ID); $ROOT_DIR = "/some/path/etc"; @DIRS = qw ( Admin Backuprequests Magic Network NewServer PatchManagement Security Serverlist ServerRetire ); # make sure vals are lowercase, to do a reverse lookup later on %ID_TO_STRING = ( c_strict => "use strict;", c_warnings => "use warnings;", c_cgi => "use cgi", c_dbi => "use dbi", ); # so we can get a clean id from the match %STRING_TO_ID = reverse %ID_TO_STRING; main(); sub main { my $match = init_match(); my $dirs = init_dirs(); my %script = (); my $wanted = sub { my $path = $File::Find::dir . "/" . $_; if (-f $path) { my %matches = (); open(FILE, "<$path") || die $!; while (my $line = <FILE>) { while ($line =~ m/($match)/ig) { # store results as hash keys so we only get one of + each $matches{$STRING_TO_ID{lc $1}} = 1; } } close FILE; $script{$path} = [ keys %matches ]; } }; find($wanted, @$dirs); # output to log warn Dumper \%script; } sub init_dirs { my $cgi = new CGI; my $dirs = [ $cgi->param('dirs') ]; $dirs->[0] ||= ""; @$dirs = @DIRS if $dirs->[0] eq "ALL"; my @fullPaths = map { "$ROOT_DIR/$_" } @$dirs; return \@fullPaths; } sub init_match { my $cgi = new CGI; my @matches = (); for my $sel (keys %ID_TO_STRING) { push @matches, $ID_TO_STRING{$sel} if $cgi->param($sel); } return join "|", @matches; }

Of course, it's a good idea to research anything that you don't understand before using it.

joecamel

Replies are listed 'Best First'.
Re^2: Questions on File::Find
by hok_si_la (Curate) on Sep 16, 2005 at 19:28 UTC
    First, thanks joecamel and revdiablo for your assistance.

    I was mainly interested in finding an efficient algorithmic approach to the problem and the answers that both of you provided sufficed. I began coding and realized the problem was a bit more complicated than I originally expected. This is why the code I posted really didn't do anything. Hopefully I was finish tidying up the script over the weekend. I will keep you posted on my progress.

    hok_si_la