gctaylor1 has asked for the wisdom of the Perl Monks concerning the following question:
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.
Which produces:#!/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";
And even though this output looks right, I feel the underlying method for generating the cards is wrong because if I move theclubs 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
statement up to the same level as the inner foreach loop I get a big ugly mess like this:print "@cards[0 .. 12]";
Thank-you for your consideration.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
|
|---|