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.


In reply to Re^2: Creating a random generator by Lady_Aleena
in thread Creating a random generator by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.