This stuff is hard to pick when you are working through this by yourself. And this exercise manages to cover a lot of points. So I figure some extra hints are ok. Below is some code to get you started with generating the deck. Un-comment the extra print statements to see how the looping works. Looping is fundamental to computer programming and it is very important to be clear on how this works.

The basic operations for dealing with arrays are: push, pop, shift, unshift and splice. Review how each one of these work. Also you can access each element in the array individually by using an index. $deck [ 0 ] is the first card.

You will have to pick a card at random from the deck. Read about the rand() function rand() function. The deck has the scalar value of "@deck" number of cards in it. The indicies of all the cards are [ 0..@deck-1]. Note this range starts at zero not one!

Once you can pick a card at random from the deck and print it, you have to figure out how you will satisfy the requirement that you can't have duplicates. I mean there is only one "2 of clubs" so you have to figure out a way to prevent it being picked twice. suggestion: think about what splice does.

Have fun and let us know how it goes! Everybody here was a beginner once.

#!/usr/bin/perl -w use strict; my @deck; foreach my $suit qw(clubs diamonds hearts spades) { #print "Working on the $suit\n"; # un-comment to watch looping foreach my $card_type (2..10,'ace','king','queen','jack') { #print " Now card=$card_type\n"; # un-comment to watch loop push @deck, "$card_type of $suit"; } } print "There are ".@deck." cards in the deck\n"; foreach my $card (@deck) { print "$card\n"; } __END__ Output....uncomment the 2 print statements in the nested loop to see exactly how the looping is done... There are 52 cards in the deck 2 of clubs 3 of clubs 4 of clubs ... etc. then diamonds, then hearts, spades...

In reply to Re: Nested foreach loops by Marshall
in thread Nested foreach loops by peterstriker

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.