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)