in reply to Golf: Improve this guy's fail . . . please!
It's a typical case of not knowing about autovivification: instead of assigning 0 to every possible hash key, and later sort out those which remained 0, you can simply leave out both steps altogether.
It is also typical in confusing ASCII with an 8-bit encoding.
A non-golfed version:
use strict; use warnings; use List::Util qw(sum); my %chars; local $/ = \1; while (<>) { $chars{$_}++ if /\w/; } my $total = sum values %chars; print "key\tcount\t%\n"; for (sort keys %chars) { printf "%s\t%d\t%.3f\n", $_, $chars{$_}, $chars{$_} / $total; }
It doesn't prompt for a filename, instead it either expects a file name on the command line, or reads from STDIN - in good old unix tradition.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Golf: Improve this guy's fail . . . please!
by ikegami (Patriarch) on Jun 30, 2009 at 22:44 UTC | |
by mr_mischief (Monsignor) on Jul 01, 2009 at 00:38 UTC | |
by shmem (Chancellor) on Jul 01, 2009 at 00:43 UTC | |
by mr_mischief (Monsignor) on Jul 01, 2009 at 01:24 UTC |