in reply to Decisiveness for Gamers

Hi Panda,

Very nice job! In addition to the things that GrandFather mentioned I'd like to encourage you to think about using hashes and references (see perlref and perldsc), because you are duplicating the core of the code for each option - you use essentially the same code for 'good' as you do for 'bad', the only thing that changes is the name of the array. By using the technique shown below you can add other moods without having to copy and paste the code block for each new mood.

use warnings; use strict; my %games = ( good => [ "nfs", "nfsII", "With the PerlMonks", "Morrowind", "TES Constructor Set" ], bad => [ "RedFactionII", "CCG", "RavenShield", "AGG" +], ); # another way to loop my $mood = ''; while( ! exists $games{$mood} ) { print "How has your day been?\n"; $mood = <STDIN>; chomp $mood; $mood = lc $mood; } my $game_aref = $games{$mood}; my $randgame = ${ $game_aref }[ rand @{ $game_aref } ]; print "You should play: $randgame\n";

To add a new mood, all you have to do is add it to the hash:

my %games = ( good => [ "nfs", "nfsII", "With the PerlMonks", "Morrowind", "TES Constructor Set" ], bad => [ "RedFactionII", "CCG", "RavenShield", "AGG" +], fantastic => [ "With the PerlMonks" ], );
Feel free to ask if you have any questions about the code.

Now for some homework:

  1. If you forget what the valid moods are, how could you have the program display a list of options?
  2. How could you add the "Error, please re-type answer" statement if the user entered an invalid option?
  3. How could you combine the solutions to problems #1 and #2 so the user gets both an error message and a list of valid moods when an invalid option is entered?
  4. Bonus question: How could you solve problem #3 without duplicating code? Hint: perlsub

I hope this helps, and that it gives you some things to think about and play with. Have fun! :-)

Replies are listed 'Best First'.
Re^2: Decisiveness for Gamers
by Panda (Scribe) on Jul 14, 2006 at 20:38 UTC
    Thanks bobf!

    I thought that you had to have a key for every value in a hash - I would have never thought of making the key a reference to an anonymous array. Those questions got me thinking as well, if I ever do make version 3.0 I'll put the answers in that.

    The thing I don't understand is how you get it to return the value of the array:

    my $game_aref = $games{$mood}; my $randgame = ${ $game_aref }[ rand @{ $game_aref } ]; print "You should play: $randgame\n";

    Way over my head, any explanation would be welcome :)

Re^2: Decisiveness for Gamers
by GrandFather (Saint) on Jul 14, 2006 at 23:28 UTC

    Combining our various suggestions and in the interests of TIMTOWTDI I offer the following:

    use warnings; use strict; my %moods = ( good => ["nfs", "nfsII", "With the PerlMonks", "Morrowind", "TES C +onstructor Set"], bad => ["RedFactionII", "CCG", "RavenShield", "AGG"], ); my $mood; while (1) { print "How has your day been?\n"; $mood = <STDIN>; chomp $mood; $mood = lc $mood; last if exists $moods{$mood}; } my $gamesRef = $moods{$mood}; my $randgame = $gamesRef->[rand @$gamesRef]; print "You should play: $randgame\n";

    which gives the hash an improved name and better facilitates a solution to homework problem 2.

    Panda should note the use of the if as a statement modifier in last if ... and the alternate syntax used for dereferencing the array ref ($gamesRef->). Note too that the {} brackets are not required for @$gamesRef.


    DWIM is Perl's answer to Gödel