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


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.