in reply to Perl subroutine update
Please next time post a new node or even a new question thread instead of editing your old node. It makes all the posts in this thread look silly since they refer to code that isn't there. Also I had problems finding your new script as there was no new node in your node list.
About your code: The subroutine looks much better now, but you forgot to correct the array problem I was talking about in Re: Perl subroutine update beginning with 'Read the section "Arrays"'. Remember, $element1 is a single value, @element1 would be an array of values. Since you pop/shift more than one value of the @cards and want to store them, you need an array.
Also, when I said, change all references of @random_card or @starting_deck to @cards in this subroutine, I really meant "in this subroutine only". You don't need to change any @random_card or @starting_deck names outside the subroutine, those are normally different variables. They may have the same name, but they are different and usually you want to use different names to make it obvious that they are different. By changing them you also introduced new bugs. But ok, we can fix it, step by step
Now, when you debug a script and you get error messages, things get really easy in a way. Usually error messages tell you all you need to know if you just make the effort to read them carefully, try to make sense of them and then correct them until the error message vanishes. Since some errors can lead to further error messages later in the script, it is best to always correct the first error message first.
So execute your script and you will see a warning that you use @starting_deck[$x] instead of $starting_deck[$x]. If you read the section about arrays, you should know that in perl you access a whole array as @array, but a single element of the array as $array[1]. What you did was use an array slice: @array[1,2,3], also legal, but perl is warning you that it makes no sense to use in this way and you probably meant $starting_deck[$x]. Change this and the warning will go away.
Now the error message "Global symbol "@cards" requires explicit package name at ./t7.pl line 14" is first. Look at line 14. There is a variable @cards you haven't introduced/declared yet, so add 'my' before it.
Now that error message should be gone. But a new warning comes up if you restart the script that says ""my" variable @cards masks earlier declaration in same scope at ./t7.pl line 17". Check out line 17. Read the error message and try to make sense out of it. It means that you declare @cards a second time. So remove the 'my' before the second '@cards'.
When you restart the script the next first error message is "Global symbol "@starting_deck" requires explicit package name at ./t7.pl line 21". If you look closely you will see that before you called the variable "@startingdeck" and not "@starting_deck". So correct that.
Next error message is "Global symbol "$card" requires explicit package name at ./t7.pl line 33". Check out line 33. You have a variable card that you didn't declare. So put 'my' in front of it and the error message vanishes
Next message is "Global symbol "@card" requires explicit package name at ./t7.pl line 36". Check out line 36. Again a typing error, you used @card instead of @cards. Change that and your script should finally be running again
If you are at that point, post your code again in a new node and we can talk about how to find the last two bugs in your script
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl subroutine update
by craziestfire73 (Initiate) on Apr 05, 2011 at 13:09 UTC | |
by jethro (Monsignor) on Apr 05, 2011 at 15:59 UTC | |
by craziestfire73 (Initiate) on Apr 05, 2011 at 17:56 UTC | |
by craziestfire73 (Initiate) on Apr 05, 2011 at 18:04 UTC | |
by jethro (Monsignor) on Apr 06, 2011 at 09:43 UTC |