determine if it really is monkeys at typewriters you are dealing with or no. supply file on the cmd line.
the script calculates the character entropy of a supplied file. nothing fancy. benchmark this agains ent, the c prog, or something fancier.
# determine char entropy from file. by wufnik.
my $fil = shift;
open (FIL, $fil) || die ("no filfor entropy calc, $!");
my $filstr = do { local $/; <FIL> };
close FIL;
my @chars = split //, $filstr;
my (%charhash, $charstot);
map { $charhash{$_}++; $charstot++ } @chars;
my @values = map { $_ / $charstot } values %charhash;
my $ent = entropy(\@values);
printf ("file %s\ncontents entropy = %20.15f\n",$fil,$ent);
sub entropy{
my ($listr, $baselog) = @_;
$baselog = 0.693147180559945 unless $baselog; # log(2)
return undef unless ref $listr;
my $sum;
my @nums = @$listr;
map { $sum += $_ * (log($_)/$baselog) } @nums;
return -$sum;
}
(edited to adopt sensible precision in printf as pointed out by
graff)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.