Title
Author
Link http://thunked.org/p/view/pub/
Created 2011-12-18 05:10:21
Expires never
Filename Triple Town Log Parser
Language Perl
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.
#!/usr/bin/perl
use strict;
use warnings;

my $logfile = shift or die "Usage: $0 <logfile to parse>\n";
open (my $fh, "<", $logfile) or die "Cannot open $logfile: $!\n";

# Total number of events that were logged
my $total_events = 0;

# Container to hold frequencies of each event
my %data;

# Human-readable event names
my %events = (
        '0' => 'Grass', '1' => 'Bush', '2' => 'Tree', '3' => 'Hut',
        'b' => 'Bear', 'n' => 'Ninja', 'x' => 'Bot', 'c' => 'Crystal'
);

# Read log file
while (my $contents = <$fh>) {
        # Ignore endline formatting
        chomp($contents);

        my @characters = split //, $contents;

        # Update total event count
        $total_events += scalar @characters;

        # Update event frequencies
        foreach (@characters) {
                if (exists $data{$_}) {
                        $data{$_}++;
                } else {
                        $data{$_} = 1;
                }
        }
}

# Spit out results
while (my ($event, $frequency) = each(%data)) {
        my $percentage = sprintf "%.3f", $frequency / $total_events * 100;
        print "$events{$event} occurred $frequency times ($percentage%)\n";
}
print "$total_events events logged.\n";