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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |