in reply to Re: Creating a random generator
in thread Creating a random generator
I was so excited, I found out about exists so I was going to apply it here. Now I can't figure out how to use it, or even if it will work with this.
#!/usr/bin/perl -w use strict; use warnings; use diagnostics; use CGI; use CGI::Carp qw(fatalsToBrowser); my %Data; #This sub will eventually go into a module. sub random { my ($name) = @_; my $array = $Data{$name}; my $pick = $array->[rand @$array]; if (ref($pick) eq 'CODE') {return $pick->();} else {return $pick;} } $Data{gen} = [ 'beholder' => {b => 1,pg => 21}, 'death kiss' => {pg => 21}, 'eye of the deep' => {pg => 21}, 'gauth' => {pg => 21}, 'spectator' => {pg => 21}, 'undead' => {pg => 21}, 'hive mother' => {pg => 25}, 'director' => {pg => 25}, 'examiner' => {pg => 25}, 'lensman' => {pg => 25}, 'overseer' => {pg => 25}, 'watcher' => {pg => 25}, ]; print random("gen") exists ($gen{$b}) ? '' : ' beholder') exists ($gen{$bk})||exists ($gen{$pg}) ? ( ' ('. exists ($gen{$bk}) ? ($gen{$bk}) : 'Monstrous Manual') exists ($gen{$pg}) ? ', page '.($gen{$pg})) .')' ) ) keys %gen(@_)
First, the item from the hash needs to be randomly selected.
Then if the variable b is in the key's hash, then print nothing. If the variable b is not in the hash, print ' beholder'.
Then if either bk or pg is in the key's hash, then print ' (' and check the following.
If bk is in the hash, print it's value. If bk is not in the hash, print 'Monstrous Manual'.
If pg is in the hash, print ', page ' and it's value. If pg is not in the hash, print nothing.
After those two are done, print a final ')'.
If neither bk nor pg are in the key's hash, then print nothing after the key.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Creating a random generator
by graff (Chancellor) on Oct 15, 2007 at 08:07 UTC |