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

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:

Update

Replies are listed 'Best First'.
Re^8: Creating a random generator
by runrig (Abbot) on Sep 13, 2007 at 21:19 UTC
    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";