-
Notifications
You must be signed in to change notification settings - Fork 0
/
freq.pl
50 lines (34 loc) · 953 Bytes
/
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
#!/usr/bin/perl
# Compute the frequency of bytes in a given input data stream
use 5.014;
use strict;
use warnings;
use experimental qw(signatures);
binmode(STDIN, ':raw');
binmode(STDOUT, ':raw');
my %table;
my $total = 0;
while (<>) {
foreach my $c (unpack("C*", $_)) {
++$table{$c};
++$total;
}
}
sub print_line ($key, $value) {
printf("%3d %2s %.2f\n", $key, chr($key) =~ /[[:print:]]/ ? chr($key) : '--', $value / $total * 100);
}
foreach my $key (sort { $a <=> $b } keys %table) {
print_line($key, $table{$key});
}
say "\nTop 10 (max):";
my @top10_max = sort { ($table{$b} <=> $table{$a}) || ($a <=> $b) } keys %table;
$#top10_max = 10;
foreach my $key (@top10_max) {
print_line($key, $table{$key});
}
say "\nTop 10 (min):";
my @top10_min = sort { ($table{$a} <=> $table{$b}) || ($a <=> $b) } keys %table;
$#top10_min = 10;
foreach my $key (@top10_min) {
print_line($key, $table{$key});
}