Without telling us what the script should do it is very difficult telling you exactly whats wrong. So I can only guess what you want

1) In your subroutine shuffle you use the global variables @starting_deck and @random_card, this probably is wrong when you call shuffle with $file as parameter. It also makes your script behave quite random, more about this later. Generally you should avoid using global variables in subroutines and give all necessary information as a parameter to the subroutine.

2) You use "my $card= shift(@_);". This is ok if you only call the subroutine with one card, but when you do "shuffle(@starting_deck)", you only get to use the first value. What you probably meant was something like this:

my @newdeck= shuffle(@starting_deck); sub shuffle { my @cards= @_; foreach (1...30) { my @cut= pop(@cards), shift (@cards), pop(@cards) ... ... push @cards, @cut; } ... return @˘ards; }

3) You try to for example substitue 'C' with Clubs'. If you do that more than once, you will produce 'Clubslubs'. So just do that once, maybe in a different subroutine (because really it has nothing to do with shuffling), before or after the shuffling.

Debugging: If your program is not working like you expect, put in 'print' statements to tell you what the program does. Even better, use Data::Dumper to print out your variables and you will get insights into its inner workings

For example you could have added to your old shuffle subroutine something like

use Data::Dumper; # <-better put this at the beginning of your script, + but works here too print Dumper(\@starting_deck);

You would have noticed then that your subroutine is only executed twice, because you change @random_card in the subroutine while your main script is stepping through @random_card in the foreach loop. @random_card gets emptied and so the foreach ends prematurely. If this is on purpose, ok, but don't think for a moment you can ever return to this script in a few months and change anything without completely breaking the script.


In reply to Re: Perl Subroutine by jethro
in thread Perl Subroutine by craziestfire73

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.