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

In reply to Re: Questions on File::Find by joecamel
in thread Questions on File::Find by hok_si_la

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.