I'm using a book to self teach programming and Perl. I'm stuck on this end-of-chapter exercise and would like advice. Maybe even just rewording it would help. This is from the end of a chapter on Control Structures, Blocks, and Compound Statements and you'll see below the emphasis is on foreach loops.

Part of the guidance I seek is affirmation that I understand the question and then guidance to get me unstuck.

The exercise is this, copied exactly from the book:

Write a script that will print 10 random number cards from a deck.

a. The script will build a deck of 52 cards by using nested foreach loops.
b. The outer loop will iterate through a list consisting of cards for each suit: clubs, diamonds, hearts, spades. The inner loop will iterate through a list for each type of card within the suit: ace, 2 through 10, jack, queen, and king. A card of each suit will be assigned to an array.
c. The rand() function will be used to get a random card from the pack. There should be no duplicates in the 10 cards selected from the deck.


My first question is about statement "b". I understand TIMTOWTDI, but I comprehend this statement as 'the outer foreach loop should create the four suits and the inner foreach loop should create the cards within the suit'.
What I think I need to do is create a multi-dimensional array so that I can access the elements later. Does that sound right?

I can get the suits created and get cards assigned to the current array(suit). Where I'm getting stuck is creating @array0 for the clubs, @array1 for the diamonds, etc. within my inner foreach loop. I'm not totally sure how that will work with the next objective (c) and the rand() function but I'm taking this one step at a time.

After trying many things I remembered the ${var} method but alas that appears to be only for scalars.

I've seen examples on the www of using references, objects, and modules, none of which have been introduced to me yet in the book. At this point the book has covered scalars, arrays, hashes, operators, contol stuctures, loops, blocks. I'm not interested in having someone solve the actual problem. I want to learn this. Maybe another way to think about the problem would help. Here's what I have right now.

#!/usr/bin/perl use strict; use warnings; my @allsuits = ( "clubs", "diamonds", "hearts", "spades" ); my @suitcards = ( "ace", 2 .. 10, "jack", "queen", "king" ); my $card; my @cards; my $suit; my @suit; my @suits; my @allfours; foreach $suit (@allsuits) { push( @suits, $suit ); @suit = $suit; print "$suit\n"; foreach $card (@suitcards) { push( @cards, "$card" ); } print "@cards[0 .. 12]"; push( @allfours, $cards[3] ); print "\n\n"; } print "All the fours of a suit - @allfours\n";
Which produces:
clubs ace 2 3 4 5 6 7 8 9 10 jack queen king diamonds ace 2 3 4 5 6 7 8 9 10 jack queen king hearts ace 2 3 4 5 6 7 8 9 10 jack queen king spades ace 2 3 4 5 6 7 8 9 10 jack queen king All the fours of a suit - 4 4 4 4
And even though this output looks right, I feel the underlying method for generating the cards is wrong because if I move the
print "@cards[0 .. 12]";
statement up to the same level as the inner foreach loop I get a big ugly mess like this:
spades ace 2 3 4 5 6 7 8 9 10 jack queen kingace 2 3 4 5 6 7 8 9 10 jack quee +n kingace 2 3 4 5 6 7 8 9 10 jack queen kingace 2 3 4 5 6 7 8 9 10 ja +ck queen kingace 2 3 4 5 6 7 8 9 10 jack queen kingace 2 3 4 5 6 7 8 +9 10 jack queen kingace 2 3 4 5 6 7 8 9 10 jack queen kingace 2 3 4 5 + 6 7 8 9 10 jack queen kingace 2 3 4 5 6 7 8 9 10 jack queen kingace +2 3 4 5 6 7 8 9 10 jack queen kingace 2 3 4 5 6 7 8 9 10 jack queen k +ingace 2 3 4 5 6 7 8 9 10 jack queen kingace 2 3 4 5 6 7 8 9 10 jack +queen king
Thank-you for your consideration.

In reply to Incrementing string arrays when used with foreach loops by gctaylor1

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.