No, the hash is just to keep track of the letters in use. You can also use the hash to tell if you're already using a letter when generating a random letter--if you've already used that random letter, try another random letter. If you think about how *you* would do it with dice, paper and pencil, you can tell the computer to do the same thing.

For example: You want to generate 8 different letters. How would we do it by hand? Like this:

  1. Get a new sheet of paper
  2. Do I have 8 letters on the sheet? If so, we're done, goto step!
  3. Roll the dice and select a random letter.
  4. Is the letter on the sheet? If not, write it on the sheet.
  5. go back to step 2.
  6. Return the list of numbers.

So for a piece of paper, I'll use a hash (it's much easier to use than individually named variables). So let's convert our manual procedure to code:

my @chars = ('A' .. 'Z'); my @hand = create_random_letter_list(); print join(", ", @hand), "\n"; sub create_random_letter_list { # 1. Get a new sheet of paper my %list; # 2. Do I have eight letters? while (8 > keys %list) { # 3. Roll the dice and select a random letter my $letter = $chars[int rand @chars]; # 4. If the letter is not on the sheet... if (! exist $list{$letter}) { # ...write it on the sheet $list{$letter}=0; } } # 5. go back to step 2 # 6. Return the list return keys %list; }

Basically, when I code, I break the problem down into smaller problems. Just keep doing that. Eventually, the problems will be simple and you can write the code for it. In your assignment, you've got several steps. You just need to break each of those sections into smaller and smaller parts. But in order to do so, that means that you have to figure out how you would do it by hand. Then you can automate it.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re^9: Need help with Peal! by roboticus
in thread Need help with Peal! by ivanzhibin

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.