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