And here you go.
I &heart programming excercises like this.
Reads files, outputs a reverse sorted list based on # of occurences. Note that some trigraphs may not be totally valid because of the s/\W//g;. for example,
"this is a line.This is another one" will yield "lineThis" as a word to be trigraphed. This is trivial to fix, though :)
Update
yes, it was.
I changed
s/ //g;
to
s/\W//g;
thanks, Albannach! (wave)
#
#
#
use strict;
my %symbol;
my %tri;
my @trikeys;
my $line;
my $ctr;
print "Processing file...\n";
while ($line = <>) {
for (split /\W/,$line) {
#discard all non-alpha. be *greedy*
s/\W//g;
(length($_)>2) && $symbol{lc($_)}++;
}
}
print "Collecting trigraphs...\n";
foreach (keys %symbol){
for ($ctr=0; $ctr <= (length($_)-3);$ctr++) {
$tri{lc(substr ($_,$ctr,3))}+= $symbol{$_};
}
}
@trikeys = sort {$tri{$b} <=> $tri{$a}} keys %tri;
print "Total Trigraphs : ",$#trikeys,"\n";
print "Trigraph\tCount\n";
foreach (@trikeys) {
print "$_\t$tri{$_}\n";
}
-
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.
|