this is really not much more (and less) use than plain ol' grep, but i want to use it as the basis for a GUI code scanner/viewer. It is set to check only .pl files, and requires a regexp on the command-line to work. The output (linux) is exactly what my_nihilist describes below.

I believe McDarren, FunkyMonk, and even sort of ikegami helped me with their feedback to ranking number of occurances and Counting in regular expressions. One of several modifications courtesy of toolic, below, lead me to eliminate an unnecessary while loop (strange...) about line 22; i left the loop in comments for those who might also find it's unnecessity worth noting.

However, it did work just fine to begin with, toolic not withstanding. There is no planet on which i would post code that i hadn't tested.

Update #2: see my final note below, in which i take Fletch and toolic's advices about variable names and whitespace.
#!/usr/bin/perl -w use strict; my $regexp = $ARGV[0] || die "\nREQUIRES COMMAND-LINE ARGUMENT\n"; my $dir; if (defined($ARGV[1])) {$dir = $ARGV[1]} else {$dir = `pwd`} chomp $dir; print "\"$regexp\" in $dir/*.pl:\n"; my (%N_index, @files); opendir(DIR, $dir) || die "can't open $dir"; @files = readdir(DIR); closedir(DIR); foreach my $pl (@files) { if ($pl =~ /\.pl$/) { my $content; open (PL, "<$dir/$pl") || die "can't open $dir/$pl"; #while (<PL>) { $content = do {local $/; <PL>}; #} that was the deleted "while loop" close (PL); my $N; if ($content =~ /$regexp/) { $_=$content; $N =()= /$regexp/g; $N_index{$pl}=$N; } } } my @rank = sort {$N_index{$b} <=> $N_index{$a}} keys %N_index; foreach (@rank) {print "\t$N_index{$_} -- $_\n";}

In reply to regexp for directory by halfcountplus

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.