I looked in my copy of "The Perl Cookbook" a very, very handy critter to have around and highly recommended by me. There are all sorts of solutions to common problems - well worth the money.

First step is a better shuffle algorithm, which is right there in Recipe 14.7, the fisher-yates algorithm. I don't see the need to keep the suits separate, generate an array (or just type it in manually) with all 52 cards, shuffle and away you go!

#!/usr/bin/perl -w use strict; use Data::Dumper; my @deck = make_deck(); fisher_yates_shuffle(\@deck); #in place shuffle print Dumper \@deck; # use pop or shift of @deck to deal a single card # or perhaps use splice to deal out multiple cards # the cards are already randomized so it doesn't matter # how you deal 'em. my @deal4 = splice(@deck,0,4); print "4 cards: @deal4\n"; #4 cards: 6_heart 9_heart J_heart Q_spade # From Perl Cookbook Recipe 14.7 # Randomizing an Array with fisher_yates_shuffle sub fisher_yates_shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } } sub make_deck #or just use a fixed array with 52 cards { my @cards = ( 2,3,4,5,6,7,8,9,10,'J','Q','K','A'); my @deck; foreach my $suit qw(spade diamond heart club) { push @deck, map{"$_"."_$suit"}@cards; } return @deck; }

In reply to Re: What I am missing here by Marshall
in thread What I am missing here by heatblazer

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.