in reply to Perl subroutine update

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 ?

Replies are listed 'Best First'.
Re^2: Perl subroutine update
by craziestfire73 (Initiate) on Apr 01, 2011 at 11:56 UTC

    Jethro,

    Thanks for your time. The reason I did not use shuffle was because, Since shuffle is a built-in function, I thought maybe it would conflict.

      There is no shuffle built-in: shuffle. But even if it had been, you could have used &shuffle.