As mentioned by
fs, hashes would probably be the best way to go. Here is one way to do it, although I'm not trying to filter out any special characters:
use strict;
my %hash;
while (<DATA>) {
chomp;
my @chars = split '';
for my $char (@chars) {
# use the next line if case doesn't matter
# $hash{lc $char}++;
$hash{$char}++;
}
print join " ", @chars, "\n";
}
my $consensus = '';
for (sort keys %hash) {
$consensus = $_ if $hash{$_} > $hash{$consensus};
print "\$hash{$_}: $hash{$_}\n";
}
print "\nChar: '$consensus' has the highest count: $hash{$consensus}!\
+n";
__DATA__
aAabbbbcdDeeeeEEefFFffffffffffff
zzzzzzzxxxxxxxxxyyYyg
Output
a A a b b b b c d D e e e e E E e f F F f f f f f f f f f f f f
z z z z z z z x x x x x x x x x y y Y y g
$hash{A}: 1
$hash{D}: 1
$hash{E}: 2
$hash{F}: 2
$hash{Y}: 1
$hash{a}: 2
$hash{b}: 4
$hash{c}: 1
$hash{d}: 1
$hash{e}: 5
$hash{f}: 13
$hash{g}: 1
$hash{x}: 9
$hash{y}: 3
$hash{z}: 7
Char: 'f' has the highest count: 13!
--Jim
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.