#!/usr/bin/perl use strict; use warnings; # This program reads log data from bears.txt and searches for instances of "You say, '***'". Each # such instance is treated as the loot for a particular bear (empty loot is just "You say, ''"). # The program then outputs a list of everything bears dropped with the probability of dropping # each thing, as well as the number of total bears killed. Note: the output file is cleared and # replaced each time the program is run. open INPUT, "bears.txt" or die $!; open OUTPUT, ">out.txt" or die $!; my $num_bears = 0; my $curr_line; my $curr_loots; my $done_with_curr_loot; # Key is the name of a loot piece; value is the number of times that piece was looted. my (%loot_count); while () { $curr_line = $_; if ($curr_line =~ /You say, '(.*)'/) { $num_bears++; $curr_loots = $1; $done_with_curr_loot = "FALSE"; while ($done_with_curr_loot eq "FALSE") { if ($curr_loots =~ /^(.*?), (.*)/) { if (exists($loot_count{$1})) { $loot_count{$1}++; } else { $loot_count{$1} = 1; } $curr_loots = $2; } else { unless ($curr_loots eq "") { if (exists($loot_count{$curr_loots})) { $loot_count{$curr_loots}++; } else { $loot_count{$curr_loots} = 1; } } $done_with_curr_loot = "TRUE"; } } } } print OUTPUT "Number of bears: $num_bears\.\n"; for (keys %loot_count) { print OUTPUT "$_ "; print OUTPUT $loot_count{$_} / $num_bears; print OUTPUT "\n"; }