Hello Monks,
I am trying to find keyword count from a file as below:
use strict;
use warnings;
my $i=0; my $j=0; my $k=0; my $l=0; my $m=0; my $n=0; my $o=0;
open(my $FILEHANDLE, "<", "Action5.c") or die "cannot open < Action5.c
+: $!";
my @eachline = <$FILEHANDLE>;
my @patterns = (qr/transaction/, qr/find/, qr/think/, qr/save_param/,
+qr/start_sub/, qr/url/, qr/submit/);
my $count = @eachline;
while(<@eachline>){
if ($_ ~~ $patterns[0]){$i++;}
elsif($_ ~~ $patterns[1]){$j++;}
elsif($_ ~~ $patterns[2]){$k++;}
elsif($_ ~~ $patterns[3]){$l++;}
elsif($_ ~~ $patterns[4]){$m++;}
elsif($_ ~~ $patterns[5]){$n++;}
elsif($_ ~~ $patterns[6]){$o++;}
}
print "Number of patterns found for".$patterns[0]." is ".$i."\n";
print "Number of patterns found for".$patterns[1]." is ".$j."\n";
print "Number of patterns found for".$patterns[2]." is ".$k."\n";
print "Number of patterns found for".$patterns[3]." is ".$l."\n";
print "Number of patterns found for".$patterns[4]." is ".$m."\n";
print "Number of patterns found for".$patterns[5]." is ".$n."\n";
print "Number of patterns found for".$patterns[6]." is ".$o."\n";
close($FILEHANDLE);
This is a very crude way,I know. Please guide me with a better code.
Regards,
Xhings
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|