Read the section "Writing subroutines" in perlintro. If you think you understand that, change the first line of your deck subroutine to

my @deck= @_;

Then change your subroutine to only operate on @deck. Don't use @random_card or @starting_deck in this subroutine, you don't need them, you only want to change and shuffle @deck. At the end of your subroutine should be

return @deck

Also you when you call your subroutine, use for example

my @starting_deck = &deck(@starting_deck);

to shuffle @starting_deck. This is how subroutines are used.

Then read the section "Arrays" in perlintro. If you think you understand that, look at your subroutine again. If you try out this:

my $element1 = pop(@deck), shift(@deck), shift(@deck); print "element1 is $element1 \n";

you will see a problem. $element1 holds only one card, but you probably wanted to pop and shift many cards into it. Consider this:

my @elements1 = (pop(@deck), shift(@deck), shift(@deck)); print "elements1 is @elements1 \n";

Now all the elements you shifted and popped off are in @elements1, ready to be added to @deck again. Why the additional parenthesis around the pops and shifts are necessary is an advanced topic, it is because ',' has more than one meaning.

PS: Don't you think something like shuffle would be a more appropriate name for your subroutine than deck ?


In reply to Re: Perl subroutine update by jethro
in thread Perl subroutine update 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.