Loading ...
Sorry, an error occurred while loading the content.

random subscripts

Expand Messages
  • Donald Korrecta
    As an absolute beginner at this language, i am stuck on what is probably so simple i ll kick myself for not seeing it. I am learning (trying!) from Ellie
    Message 1 of 7 , Mar 19, 2008
    • 0 Attachment
      As an absolute beginner at this language, i am stuck
      on what is probably so simple i'll kick myself for not
      seeing it.
      I am learning (trying!) from Ellie Quigley's Perl by
      Example 4th Ed.
      One exercise asks the programmer to build a deck of
      cards (easy!).
      My hitch is in trying to get 10 cards at random.
      I can get ten cards but, they are all the same card.
      How does one get random subscripts with each iteration
      of the loop?
      Second half of prog:

      foreach $i (1..10) {
      $card = $deck[rand @deck];
      print "Card $i is the $card";
      }
      What am I overlooking?


      ____________________________________________________________________________________
      Never miss a thing. Make Yahoo your home page.
      http://www.yahoo.com/r/hs
    • merlyn@stonehenge.com
      ... Donald I can get ten cards but, they are all the same card. Your code wouldn t get the same card each time. Maybe you mean sometimes I get the same
      Message 2 of 7 , Mar 19, 2008
      • 0 Attachment
        >>>>> "Donald" == Donald Korrecta <draktrax@...> writes:

        Donald> I can get ten cards but, they are all the same card.

        Your code wouldn't get the same card each time. Maybe you mean
        "sometimes I get the same card".

        Unless somehow you have only one card in @deck.

        So perhaps you could post the code that has the behavior above,
        or confirm the other two things I just said. Also show
        how you initialize @deck.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      • Donald Korrecta
        ... The entire prog: foreach $b ( spades , hearts , clubs , diamonds ) { foreach $a ( ace , 2..10, jack , queen , king ) { $c=0; $deck[&c]= $a of $b ;
        Message 3 of 7 , Mar 19, 2008
        • 0 Attachment
          --- merlyn@... wrote:

          > >>>>> "Donald" == Donald Korrecta
          > <draktrax@...> writes:
          >
          > Donald> I can get ten cards but, they are all the
          > same card.
          >
          > Your code wouldn't get the same card each time.
          > Maybe you mean
          > "sometimes I get the same card".
          >
          > Unless somehow you have only one card in @deck.
          >
          > So perhaps you could post the code that has the
          > behavior above,
          > or confirm the other two things I just said. Also
          > show
          > how you initialize @deck.
          >
          > --
          > Randal L. Schwartz - Stonehenge Consulting Services,
          > Inc. - +1 503 777 0095
          > <merlyn@...>
          > <URL:http://www.stonehenge.com/merlyn/>
          > Perl/Unix/security consulting, Technical writing,
          > Comedy, etc. etc.
          > See PerlTraining.Stonehenge.com for onsite and
          > open-enrollment Perl training!
          >
          The entire prog:
          foreach $b ("spades", "hearts", "clubs", "diamonds") {
          foreach $a ("ace", 2..10, "jack", "queen", "king") {
          $c=0;
          $deck[&c]= "$a of $b";
          $c++;
          }
          }
          foreach $i (1..10) }
          $card=$deck[rand @deck];
          print "Card $i is the $card;
          }

          if i print out the result of the nested loops
          i get a list of all cards. the prog returns only the
          king of diamonds. what am i missing?


          ____________________________________________________________________________________
          Never miss a thing. Make Yahoo your home page.
          http://www.yahoo.com/r/hs
        • yaswanth krishna
          Move that initialization $c = 0; before the foreach loop. Every time index 1 is getting initialized and finally that array is having only one element whch
          Message 4 of 7 , Mar 19, 2008
          • 0 Attachment
            Move that initialization"$c = 0;" before the foreach loop. Every time index
            '1' is getting initialized and finally that array is having only one element
            whch is initialized at last :)

            -Yaswanth

            On Thu, Mar 20, 2008 at 10:26 AM, Donald Korrecta <draktrax@...>
            wrote:

            >
            > --- merlyn@... <merlyn%40stonehenge.com> wrote:
            >
            > > >>>>> "Donald" == Donald Korrecta
            > > <draktrax@... <draktrax%40yahoo.com>> writes:
            > >
            > > Donald> I can get ten cards but, they are all the
            > > same card.
            > >
            > > Your code wouldn't get the same card each time.
            > > Maybe you mean
            > > "sometimes I get the same card".
            > >
            > > Unless somehow you have only one card in @deck.
            > >
            > > So perhaps you could post the code that has the
            > > behavior above,
            > > or confirm the other two things I just said. Also
            > > show
            > > how you initialize @deck.
            > >
            > > --
            > > Randal L. Schwartz - Stonehenge Consulting Services,
            > > Inc. - +1 503 777 0095
            > > <merlyn@... <merlyn%40stonehenge.com>>
            > > <URL:http://www.stonehenge.com/merlyn/>
            > > Perl/Unix/security consulting, Technical writing,
            > > Comedy, etc. etc.
            > > See PerlTraining.Stonehenge.com for onsite and
            > > open-enrollment Perl training!
            > >
            > The entire prog:
            > foreach $b ("spades", "hearts", "clubs", "diamonds") {
            > foreach $a ("ace", 2..10, "jack", "queen", "king") {
            > $c=0;
            > $deck[&c]= "$a of $b";
            > $c++;
            > }
            > }
            > foreach $i (1..10) }
            > $card=$deck[rand @deck];
            > print "Card $i is the $card;
            > }
            >
            > if i print out the result of the nested loops
            > i get a list of all cards. the prog returns only the
            > king of diamonds. what am i missing?
            >
            > __________________________________________________________
            > Never miss a thing. Make Yahoo your home page.
            > http://www.yahoo.com/r/hs
            >
            >


            [Non-text portions of this message have been removed]
          • Jenda Krynicky
            From: Donald Korrecta ... I believe you meant $deck[$c]= $a of $b ; your code would attempt to call the c() subroutine and thus should
            Message 5 of 7 , Mar 20, 2008
            • 0 Attachment
              From: Donald Korrecta <draktrax@...>
              > The entire prog:
              > foreach $b ("spades", "hearts", "clubs", "diamonds") {
              > foreach $a ("ace", 2..10, "jack", "queen", "king") {
              > $c=0;
              > $deck[&c]= "$a of $b";

              I believe you meant
              $deck[$c]= "$a of $b";

              your code would attempt to call the c() subroutine and thus should
              return an error that there is no such function.

              > $c++;
              > }
              > }
              > foreach $i (1..10) }
              > $card=$deck[rand @deck];
              > print "Card $i is the $card;
              > }
              >
              > if i print out the result of the nested loops
              > i get a list of all cards. the prog returns only the
              > king of diamonds. what am i missing?

              The first thing you are missing is

              use strict;
              use warnings;
              # and possibly
              # no warnings 'uninitialized'; # I find this particular warning
              annoying.

              then you are missing the variable declarations.

              foreach my $b ("spades", "hearts", "clubs", "diamonds") {
              foreach my $a ("ace", 2..10, "jack", "queen", "king") {
              my $c=0;
              $deck[$c]= "$a of $b";
              $c++;
              }
              }
              foreach my $i (1..10) }
              my $card=$deck[rand @deck];
              print "Card $i is the $card\n";
              }

              this might help you to see the problem, you reset the $c counter in
              each iteration of the inner foreach loop, thus you only ever set
              $deck[0]. Which you could easiy find out by printing the @deck to
              make sure it's initialized correctly:

              use Data::Dumper;
              print Dumper(\@deck);


              Next thing, you should not use variables $a and $b outside sort{}.
              They are a bit special. Besides $a and $b is not very informative, is
              it? What does "a" mean? Nothing! Use meaningful variable names!

              Also, if you want to add a value at the end of an array you do not
              need to keep a counter, just push() the value into the @array:


              use strict;
              use warnings;
              no warnings 'uninitialized';

              my @deck;
              foreach my $color ("spades", "hearts", "clubs", "diamonds") {
              foreach my $value ("ace", 2..10, "jack", "queen", "king") {
              push @deck, "$value of $color";
              }
              }
              # use Data::Dumper;
              # print Dumper(\@deck);

              foreach my $i (1..10) }
              my $card=$deck[rand @deck];
              print "Card $i is the $card\n";
              }


              HTH, Jenda
              ===== Jenda@... === http://Jenda.Krynicky.cz =====
              When it comes to wine, women and song, wizards are allowed
              to get drunk and croon as much as they like.
              -- Terry Pratchett in Sourcery
            • Donald Korrecta
              ... lesson in WATCH WHAT YOUR DOING!!! Such an elementary mistake! I knew I d kick myself as soon as I saw the solution. Again THANK YOU!!!
              Message 6 of 7 , Mar 20, 2008
              • 0 Attachment
                --- yaswanth krishna <yaswanth.krishna@...>
                wrote:

                > Move that initialization"$c = 0;" before the foreach
                > loop. Every time index
                > '1' is getting initialized and finally that array is
                > having only one element
                > whch is initialized at last :)
                >
                > -Yaswanth
                >
                > On Thu, Mar 20, 2008 at 10:26 AM, Donald Korrecta
                > <draktrax@...>
                > wrote:
                >
                > >
                > > --- merlyn@...
                > <merlyn%40stonehenge.com> wrote:
                > >
                > > > >>>>> "Donald" == Donald Korrecta
                > > > <draktrax@... <draktrax%40yahoo.com>>
                > writes:
                > > >
                > > > Donald> I can get ten cards but, they are all
                > the
                > > > same card.
                > > >
                > > > Your code wouldn't get the same card each time.
                > > > Maybe you mean
                > > > "sometimes I get the same card".
                > > >
                > > > Unless somehow you have only one card in @deck.
                > > >
                > > > So perhaps you could post the code that has the
                > > > behavior above,
                > > > or confirm the other two things I just said.
                > Also
                > > > show
                > > > how you initialize @deck.
                > > >
                > > > --
                > > > Randal L. Schwartz - Stonehenge Consulting
                > Services,
                > > > Inc. - +1 503 777 0095
                > > > <merlyn@...
                > <merlyn%40stonehenge.com>>
                > > > <URL:http://www.stonehenge.com/merlyn/>
                > > > Perl/Unix/security consulting, Technical
                > writing,
                > > > Comedy, etc. etc.
                > > > See PerlTraining.Stonehenge.com for onsite and
                > > > open-enrollment Perl training!
                > > >
                > > The entire prog:
                > > foreach $b ("spades", "hearts", "clubs",
                > "diamonds") {
                > > foreach $a ("ace", 2..10, "jack", "queen", "king")
                > {
                > > $c=0;
                > > $deck[&c]= "$a of $b";
                > > $c++;
                > > }
                > > }
                > > foreach $i (1..10) }
                > > $card=$deck[rand @deck];
                > > print "Card $i is the $card;
                > > }
                > >
                > > if i print out the result of the nested loops
                > > i get a list of all cards. the prog returns only
                > the
                > > king of diamonds. what am i missing?
                > >
                > >
                >
                _____________________________________________________
                > > Never miss a thing. Make Yahoo your home page.
                > > http://www.yahoo.com/r/hs
                > >
                > >
                >
                >
                > [Non-text portions of this message have been
                > removed]
                >
                > Thank you. Thank you. Thank you for the valuable
                lesson in "WATCH WHAT YOUR DOING!!!"
                Such an elementary mistake!
                I knew I'd kick myself as soon as I saw
                the solution. Again THANK YOU!!!



                ____________________________________________________________________________________
                Be a better friend, newshound, and
                know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
              • Donald Korrecta
                ... in the actual script the variables are called suit and rank . Some of your response,though, is a little past where I am. I do thank you for your lesson
                Message 7 of 7 , Mar 20, 2008
                • 0 Attachment
                  --- Jenda Krynicky <Jenda@...> wrote:

                  > From: Donald Korrecta <draktrax@...>
                  > > The entire prog:
                  > > foreach $b ("spades", "hearts", "clubs",
                  > "diamonds") {
                  > > foreach $a ("ace", 2..10, "jack", "queen",
                  > "king") {
                  > > $c=0;
                  > > $deck[&c]= "$a of $b";
                  >
                  > I believe you meant
                  > $deck[$c]= "$a of $b";
                  >
                  > your code would attempt to call the c() subroutine
                  > and thus should
                  > return an error that there is no such function.
                  >
                  > > $c++;
                  > > }
                  > > }
                  > > foreach $i (1..10) }
                  > > $card=$deck[rand @deck];
                  > > print "Card $i is the $card;
                  > > }
                  > >
                  > > if i print out the result of the nested loops
                  > > i get a list of all cards. the prog returns only
                  > the
                  > > king of diamonds. what am i missing?
                  >
                  > The first thing you are missing is
                  >
                  > use strict;
                  > use warnings;
                  > # and possibly
                  > # no warnings 'uninitialized'; # I find this
                  > particular warning
                  > annoying.
                  >
                  > then you are missing the variable declarations.
                  >
                  > foreach my $b ("spades", "hearts", "clubs",
                  > "diamonds") {
                  > foreach my $a ("ace", 2..10, "jack", "queen",
                  > "king") {
                  > my $c=0;
                  > $deck[$c]= "$a of $b";
                  > $c++;
                  > }
                  > }
                  > foreach my $i (1..10) }
                  > my $card=$deck[rand @deck];
                  > print "Card $i is the $card\n";
                  > }
                  >
                  > this might help you to see the problem, you reset
                  > the $c counter in
                  > each iteration of the inner foreach loop, thus you
                  > only ever set
                  > $deck[0]. Which you could easiy find out by printing
                  > the @deck to
                  > make sure it's initialized correctly:
                  >
                  > use Data::Dumper;
                  > print Dumper(\@deck);
                  >
                  >
                  > Next thing, you should not use variables $a and $b
                  > outside sort{}.
                  > They are a bit special. Besides $a and $b is not
                  > very informative, is
                  > it? What does "a" mean? Nothing! Use meaningful
                  > variable names!
                  >
                  > Also, if you want to add a value at the end of an
                  > array you do not
                  > need to keep a counter, just push() the value into
                  > the @array:
                  >
                  >
                  > use strict;
                  > use warnings;
                  > no warnings 'uninitialized';
                  >
                  > my @deck;
                  > foreach my $color ("spades", "hearts", "clubs",
                  > "diamonds") {
                  > foreach my $value ("ace", 2..10, "jack", "queen",
                  > "king") {
                  > push @deck, "$value of $color";
                  > }
                  > }
                  > # use Data::Dumper;
                  > # print Dumper(\@deck);
                  >
                  > foreach my $i (1..10) }
                  > my $card=$deck[rand @deck];
                  > print "Card $i is the $card\n";
                  > }
                  >
                  >
                  > HTH, Jenda
                  > ===== Jenda@... === http://Jenda.Krynicky.cz
                  > =====
                  > When it comes to wine, women and song, wizards are
                  > allowed
                  > to get drunk and croon as much as they like.
                  > -- Terry Pratchett in Sourcery
                  >
                  >

                  in the actual script the variables are called "suit"
                  and "rank". Some of your response,though, is a little
                  past where I am. I do thank you for your lesson and
                  will keep your correspondence for future reference.
                  a most grateful newbie,
                  draktrax


                  ____________________________________________________________________________________
                  Be a better friend, newshound, and
                  know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
                Your message has been successfully submitted and would be delivered to recipients shortly.