in reply to Re^4: Creating a random generator
in thread Creating a random generator

To give you all an idea of what the output is to look like, here is each item of the array.

Replies are listed 'Best First'.
Re^6: Creating a random generator
by wfsp (Abbot) on Sep 08, 2007 at 11:45 UTC
    I've reduce the size of the table for brevity.

    #!/usr/bin/perl use strict; use warnings; my $beholder_default = 'beholder'; my $param = get_param(); my %monster_lookup = ( 'beholder' => { book => 'Monstrous Manual', beholder => 1, page => 21, }, 'death kiss' => { book => 'Monstrous Manual', beholder => 0, page => 21, }, ); # this saves duplicating data in your code my @monsters = keys %monster_lookup; my $monster; if ($param){ $monster = $beholder_default; } else{ my $rand = int rand @monsters; $monster = $monsters[$rand]; } my $book = $monster_lookup{$monster}{book}; my $page = $monster_lookup{$monster}{page}; my $beholder = $monster_lookup{$monster}{beholder}; if (not $beholder){ print "$monster "; } print "beholder ($book, pg $page)\n"; sub get_param { # get your input from somewhere my $param = 0; return $param; }
      Here is the hash as I would like it with the "default" book as a scalar, and used in the hash. I hope that works.
      my $MM = "Monstrous Manual" my %monster_lookup = ( 'beholder' => { book => $MM, beholder => 1, page => 21, }, 'death kiss' => { book => $MM, beholder => 0, page => 21, }, 'eye of the deep' => { book => $MM, beholder => 0, page => 21, }, 'gauth' => { book => $MM, beholder => 0, page => 21, }, 'spectator' => { book => $MM, beholder => 0, page => 21, }, 'undead' => { book => $MM, beholder => 0, page => 21, }, 'hive mother' => { book => $MM, beholder => 0, page => 25, }, 'director' => { book => $MM, beholder => 0, page => 25, }, 'examiner' => { book => $MM, beholder => 0, page => 25, }, 'lensman' => { book => $MM, beholder => 0, page => 25, }, 'overseer' => { book => $MM, beholder => 0, page => 25, }, 'watcher' => { book => $MM, beholder => 0, page => 25, }, );

      Would Perl be okay with the following?

      my $MM = "Monstrous Manual" my %monster_lookup = ( 'beholder' => {book => $MM,beholder => 1,page => 21,}, 'death kiss' => {book => $MM,beholder => 0,page => 21,}, 'eye of the deep' => {book => $MM,beholder => 0,page => 21,}, 'gauth' => {book => $MM,beholder => 0,page => 21,}, 'spectator' => {book => $MM,beholder => 0,page => 21,}, 'undead' => {book => $MM,beholder => 0,page => 21,}, 'hive mother' => {book => $MM,beholder => 0,page => 25,}, 'director' => {book => $MM,beholder => 0,page => 25,}, 'examiner' => {book => $MM,beholder => 0,page => 25,}, 'lensman' => {book => $MM,beholder => 0,page => 25,}, 'overseer' => {book => $MM,beholder => 0,page => 25,}, 'watcher' => {book => $MM,beholder => 0,page => 25,}, );

      What I really want with the book key is that if it is empty, then the default book, else what is in the book in the key.

      The argument between using package or lexical variables can continue at a later time.

      Corrections:

      • quoting
      • hash item without keys.
      • copy/paste errors

      Update

      • 2nd bit of code without the whitespace
        If you are obsessed with saving space, you could do something like:
        # In case you want to access the array mnemonically. # Depending on your version of perl, you might need to # declare one constant at a time. use constant { BOOK => 0, BEHOLDER => 1, PAGE => 2 }; my $MM = "Monstrous Manual"; my %monster_lookup = ( 'beholder' => [$MM, 1, 21], 'death kiss' => [$MM, 0, 21], 'eye of the deep' => [$MM, 0, 21], #etc... ); my $monster_type = 'beholder'; my $monster = $monster_lookup{$monster_type}; printf "Monster: %s Book: %s Beholder: %s Page: %s\n", $monster_type, +@$monster; # E.g. accessing one attribute at a time print "$monster->[BOOK]\n";