Advertisement
Guest User

bearparser.pl

a guest
Feb 6th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.82 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 $num_bears = 0;
  15.  
  16. my $curr_line;
  17. my $curr_loots;
  18. my $done_with_curr_loot;
  19.  
  20. # Key is the name of a loot piece; value is the number of times that piece was looted.
  21. my (%loot_count);
  22.  
  23. while (<INPUT>)
  24. {
  25.    $curr_line = $_;
  26.    if ($curr_line =~ /You say, '(.*)'/)
  27.    {
  28.       $num_bears++;
  29.       $curr_loots = $1;
  30.       $done_with_curr_loot = "FALSE";
  31.       while ($done_with_curr_loot eq "FALSE")
  32.       {
  33.          if ($curr_loots =~ /^(.*?), (.*)/)
  34.          {
  35.             if (exists($loot_count{$1}))
  36.             {
  37.                $loot_count{$1}++;
  38.             }
  39.             else
  40.             {
  41.                $loot_count{$1} = 1;
  42.             }
  43.             $curr_loots = $2;
  44.          }
  45.          else
  46.          {
  47.             unless ($curr_loots eq "")
  48.             {
  49.                if (exists($loot_count{$curr_loots}))
  50.                {
  51.                   $loot_count{$curr_loots}++;
  52.                }
  53.                else
  54.                {
  55.                   $loot_count{$curr_loots} = 1;
  56.                }
  57.             }
  58.             $done_with_curr_loot = "TRUE";
  59.          }
  60.       }
  61.    }
  62. }
  63.  
  64. print OUTPUT "Number of bears: $num_bears\.\n";
  65. for (keys %loot_count)
  66. {
  67.    print OUTPUT "$_ ";
  68.    print OUTPUT $loot_count{$_} / $num_bears;
  69.    print OUTPUT "\n";
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement