Advertisement
Guest User

bearparser.pl

a guest
Feb 19th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.27 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. # This program reads log data from bears.txt and searches for instances of "You say, '***'". Each
  6. # such instance is treated as the loot for a particular bear (empty loot is just "You say, ''").
  7. # The program then outputs a list of everything bears dropped with the probability of dropping
  8. # each thing, as well as the number of total bears killed. Note: the output file is cleared and
  9. # replaced each time the program is run.
  10.  
  11. open INPUT, "bears.txt" or die $!;
  12. open OUTPUT, ">out.txt" or die $!;
  13.  
  14. my $curr_line;
  15. my $curr_monster = "NONE";
  16. my $curr_loots;
  17. my $curr_loot_piece;
  18. my $done_with_curr_loot;
  19. my $found_loot = "FALSE";
  20. my $curr_money;
  21.  
  22. my %loot_count;
  23. # This is a hash of hashes: the key is the name of a monster, while the value is the following hash:
  24. # Key is the name of a loot piece; value is the number of times that piece was looted.
  25.  
  26. my %monster_count;
  27. # Key is the name of a monster; value is number of such monsters found.
  28.  
  29. my %money_count;
  30. # Key is the name of a monster; value is total amount of money dropped (in copper)
  31.  
  32. while (<INPUT>)
  33. {
  34.    $curr_line = $_;
  35.    
  36.    # If we are not currently looking at a monster, look for the next instance of slaying one.
  37.    if ($curr_monster eq "NONE" && $curr_line =~ /You have slain (.*)!/)
  38.    {
  39.       $curr_monster = $1;
  40.       if (exists($monster_count{$curr_monster}))
  41.       {
  42.          $monster_count{$curr_monster}++;
  43.       }
  44.       else
  45.       {
  46.          $monster_count{$curr_monster} = 1;
  47.       }
  48.       $found_loot = "FALSE";
  49.       $curr_money = 0;
  50.    }
  51.    elsif ($curr_monster ne "NONE" && $found_loot eq "FALSE" &&
  52.           $curr_line =~ /You receive (.*) from the corpse./)
  53.    {
  54.       if ($curr_line =~ /(\d+) copper/)
  55.       {
  56.          $curr_money += $1;
  57.       }
  58.       if ($curr_line =~ /(\d+) silver/)
  59.       {
  60.          $curr_money += 10 * $1;
  61.       }
  62.       if ($curr_line =~ /(\d+) gold/)
  63.       {
  64.          $curr_money += 100 * $1;
  65.       }
  66.       if ($curr_line =~ /(\d+) platinum/)
  67.       {
  68.          $curr_money += 1000 * $1;
  69.       }
  70.    }
  71.    elsif ($curr_monster ne "NONE" && $curr_line =~ /You say, '(.*)'/)
  72.    {
  73.       if (exists($money_count{$curr_monster}))
  74.       {
  75.          $money_count{$curr_monster} += $curr_money;
  76.       }
  77.       else
  78.       {
  79.          $money_count{$curr_monster} = $curr_money;
  80.       }
  81.      
  82.       $curr_loots = $1;
  83.       $done_with_curr_loot = "FALSE";
  84.       while ($done_with_curr_loot eq "FALSE")
  85.       {
  86.          if ($curr_loots =~ /^(.*?), (.*)/)
  87.          {
  88.             $curr_loot_piece = $1;
  89.             if (exists($loot_count{$curr_monster}))
  90.             {
  91.                if (exists($loot_count{$curr_monster}{$curr_loot_piece}))
  92.                {
  93.                   $loot_count{$curr_monster}{$curr_loot_piece}++;
  94.                }
  95.                else
  96.                {
  97.                   $loot_count{$curr_monster}{$curr_loot_piece} = 1;
  98.                }
  99.             }
  100.             else
  101.             {
  102.                $loot_count{$curr_monster} = {$curr_loot_piece => 1};
  103.             }
  104.             $curr_loots = $2;
  105.          }
  106.          else
  107.          {
  108.             unless ($curr_loots eq "")
  109.             {
  110.            if (exists($loot_count{$curr_monster}))
  111.            {
  112.               if (exists($loot_count{$curr_monster}{$curr_loots}))
  113.               {
  114.                  $loot_count{$curr_monster}{$curr_loots}++;
  115.               }
  116.               else
  117.               {
  118.                  $loot_count{$curr_monster}{$curr_loots} = 1;
  119.               }
  120.            }
  121.            else
  122.            {
  123.               $loot_count{$curr_monster} = {$curr_loots => 1};
  124.            }
  125.             }
  126.             $done_with_curr_loot = "TRUE";
  127.          }
  128.       }
  129.       $curr_monster = "NONE";
  130.       $found_loot = "TRUE";
  131.    }
  132. }
  133.  
  134. for (keys %loot_count)
  135. {
  136.    $curr_monster = $_;
  137.    print OUTPUT "Number of $curr_monster: ";
  138.    print OUTPUT $monster_count{$curr_monster};
  139.    print OUTPUT "\n";
  140.  
  141.    print OUTPUT "Average coin loot: ";
  142.    print OUTPUT $money_count{$curr_monster} / ($monster_count{$curr_monster} * 1000);
  143.    print OUTPUT "\n";
  144.  
  145.    for (keys($loot_count{$curr_monster}))
  146.    {
  147.       print OUTPUT "$_ ";
  148.       print OUTPUT $loot_count{$curr_monster}{$_} / $monster_count{$curr_monster};
  149.       print OUTPUT "\n";
  150.    }
  151.    
  152.    print OUTPUT "\n";
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement