Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: help with "use strict" and code organization

by thezip (Vicar)
on Apr 24, 2008 at 06:16 UTC ( #682565=note: print w/replies, xml ) Need Help??

in reply to help with "use strict" and code organization

MrFloydFreak42 ,

  • Before you can refer to any of the variables (ie. $savegame, $deathcnt, etc.), you will need to declare each of them using "my".

  • Since your usage for each of these variables is as an integer, you'd do well to explicitly initialize them to zero:
    use strict; use warnings; my $savegame = 0; my $deathcnt = 0; ... then initialize the rest of your vars ... ... read the log file and accumulate the counts for each variable ...
  • Once you have accumulated the various counts, you can do something like:
    my $msgformat = "You have obtained %d %s.\n"; if ($shotgobt) { printf $msgformat, $shotgobt, "shotguns") }; if ($armorobt) { printf $msgformat, $armorobt, "Armors") }; ... do the same type of thing for the rest of the messages ...
  • One radical change you could do is to completely eliminate the scalar variables all together, and just use a hash to accumulate the values, such as:
    my %stats = ( 'clips' => 6, 'boxes of ammo' => 2, 'shells' => 47, 'boxes of shells' => 3, 'shotguns' => 2, 'Armors' => 2, 'health bonuses' => 1, 'medikits' => 4, 'stimpacks' => 2, 'yellow keycards' => 1, 'backpacks' => 1, 'berserk packs' => 1, );
    As you parse your log file, you'll just accumulate the values into the appropriate hash keys. Then it's just a matter of printing out the keys and their values, like:
    my $msgformat = "You have obtained %d %s.\n"; for my $stat (keys %stats) { printf $msgformat, $stats{$stat}, $stat; }
  • You'll notice that some keys don't exist in the hash table. That's because values never accumulated for them during your parsing

  • If you need to show the keys in a particular order, you could create an array of the keynames and use that array instead of keys %stats

  • Then there are also the cases where the message labels don't match the "You have obtained" format. These are also not stored in the hash, but can be stored in their own scalars and handled separately.

Your wish is my commandline.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://682565]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-05-25 02:00 GMT
Find Nodes?
    Voting Booth?