Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 $curr_line;
- my $curr_monster = "NONE";
- my $curr_loots;
- my $curr_loot_piece;
- my $done_with_curr_loot;
- my $found_loot = "FALSE";
- my $curr_money;
- my %loot_count;
- # This is a hash of hashes: the key is the name of a monster, while the value is the following hash:
- # Key is the name of a loot piece; value is the number of times that piece was looted.
- my %monster_count;
- # Key is the name of a monster; value is number of such monsters found.
- my %money_count;
- # Key is the name of a monster; value is total amount of money dropped (in copper)
- while (<INPUT>)
- {
- $curr_line = $_;
- # If we are not currently looking at a monster, look for the next instance of slaying one.
- if ($curr_monster eq "NONE" && $curr_line =~ /You have slain (.*)!/)
- {
- $curr_monster = $1;
- if (exists($monster_count{$curr_monster}))
- {
- $monster_count{$curr_monster}++;
- }
- else
- {
- $monster_count{$curr_monster} = 1;
- }
- $found_loot = "FALSE";
- $curr_money = 0;
- }
- elsif ($curr_monster ne "NONE" && $found_loot eq "FALSE" &&
- $curr_line =~ /You receive (.*) from the corpse./)
- {
- if ($curr_line =~ /(\d+) copper/)
- {
- $curr_money += $1;
- }
- if ($curr_line =~ /(\d+) silver/)
- {
- $curr_money += 10 * $1;
- }
- if ($curr_line =~ /(\d+) gold/)
- {
- $curr_money += 100 * $1;
- }
- if ($curr_line =~ /(\d+) platinum/)
- {
- $curr_money += 1000 * $1;
- }
- }
- elsif ($curr_monster ne "NONE" && $curr_line =~ /You say, '(.*)'/)
- {
- if (exists($money_count{$curr_monster}))
- {
- $money_count{$curr_monster} += $curr_money;
- }
- else
- {
- $money_count{$curr_monster} = $curr_money;
- }
- $curr_loots = $1;
- $done_with_curr_loot = "FALSE";
- while ($done_with_curr_loot eq "FALSE")
- {
- if ($curr_loots =~ /^(.*?), (.*)/)
- {
- $curr_loot_piece = $1;
- if (exists($loot_count{$curr_monster}))
- {
- if (exists($loot_count{$curr_monster}{$curr_loot_piece}))
- {
- $loot_count{$curr_monster}{$curr_loot_piece}++;
- }
- else
- {
- $loot_count{$curr_monster}{$curr_loot_piece} = 1;
- }
- }
- else
- {
- $loot_count{$curr_monster} = {$curr_loot_piece => 1};
- }
- $curr_loots = $2;
- }
- else
- {
- unless ($curr_loots eq "")
- {
- if (exists($loot_count{$curr_monster}))
- {
- if (exists($loot_count{$curr_monster}{$curr_loots}))
- {
- $loot_count{$curr_monster}{$curr_loots}++;
- }
- else
- {
- $loot_count{$curr_monster}{$curr_loots} = 1;
- }
- }
- else
- {
- $loot_count{$curr_monster} = {$curr_loots => 1};
- }
- }
- $done_with_curr_loot = "TRUE";
- }
- }
- $curr_monster = "NONE";
- $found_loot = "TRUE";
- }
- }
- for (keys %loot_count)
- {
- $curr_monster = $_;
- print OUTPUT "Number of $curr_monster: ";
- print OUTPUT $monster_count{$curr_monster};
- print OUTPUT "\n";
- print OUTPUT "Average coin loot: ";
- print OUTPUT $money_count{$curr_monster} / ($monster_count{$curr_monster} * 1000);
- print OUTPUT "\n";
- for (keys($loot_count{$curr_monster}))
- {
- print OUTPUT "$_ ";
- print OUTPUT $loot_count{$curr_monster}{$_} / $monster_count{$curr_monster};
- print OUTPUT "\n";
- }
- print OUTPUT "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement