-
Notifications
You must be signed in to change notification settings - Fork 34
/
chr_freq.pl
executable file
·55 lines (43 loc) · 1.06 KB
/
chr_freq.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 30 March 2012
# https://github.com/trizen
# Count Character Frequency in a file
use 5.010;
use strict;
use warnings;
use open IO => ':utf8', ':std';
my %table;
my %memoize;
my %white_spaces = (
ord("\r") => q{\r},
ord("\n") => q{\n},
ord("\f") => q{\f},
ord("\t") => q{\t},
ord(" ") => q{' '},
);
my $file = shift // $0;
open my $fh, '<', $file or die "Unable to open $file: $!";
while (defined(my $char = getc $fh)) {
++$table{
$memoize{$char} // do {
$memoize{$char} = ord $char;
}
};
}
close $fh;
$= = 80;
format STDOUT_TOP =
CHR ORD USED
-----------------------------------
.
my $key;
format STDOUT =
@>> @>>>>>> @>>>>>>
$white_spaces{$key} // chr $key, $key, $table{$key}
.
foreach $key (sort { $table{$b} <=> $table{$a} } keys %table) {
write;
}
say "\nUnique characters used: ", scalar keys %table;