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

no repeats

Expand Messages
  • draktrax
    How is this done: Remove randomly chosen elements from an array so that those elements cannot be chosen again. Ex: If I have a deck of cards, and would like to
    Message 1 of 8 , May 28 11:45 AM
    • 0 Attachment
      How is this done:

      Remove randomly chosen elements from an array so that those elements
      cannot be chosen again.
      Ex:
      If I have a deck of cards, and would like to ramdomly choose ten;
      what syntax ensures that as each card is chosen it is removed from the
      deck so as not to be chosen again?

      I can get ten cards but, am stuck on no repeats.
    • C.Y./J.E. Cripps
      ... Doubtless there s more than one! ... Think on the path by which this conundrum presented itself to you. Consider your past acquaintance with Perl, and the
      Message 2 of 8 , May 28 8:34 PM
      • 0 Attachment
        > How is this done:

        Doubtless there's more than one!

        > Remove randomly chosen elements from an array so that those elements
        > cannot be chosen again.
        > Ex:
        > If I have a deck of cards, and would like to ramdomly choose ten;
        > what syntax ensures that as each card is chosen it is removed from the
        > deck so as not to be chosen again?

        > I can get ten cards but, am stuck on no repeats.


        Think on the path by which this conundrum presented itself to you.
        Consider your past acquaintance with Perl, and the circumstances
        in which that acquaintance was made, for therein, I suspect,
        lies the clues you desire.

        We, whose path might not have been yours, cannot view yours
        in retrospect.

        And as is the way with Perl, there's more than one way to
        reacquaint oneself with ones past acquaintance.

        Consult your reference book. You do have a book don't you?
        Did you make any marks in the margins, ToC or index?

        One can buy index cards, write an operator or variable or other
        Perl construct on them, and then on the other side make a note
        of its use. If done as one progresses, one can pick out a card
        (in any order or randomly, if one prefers) and review it. One
        thereby has some record of what one has done previously.

        You might consult your class notes, if such exist. (It's highly
        recommended to take notes, as learning is facilitated by using
        more than one sensory pathway.)

        One can look through one's previous code. It's also recommended
        to put many comments in your codes, especially while beginning.
        perl -e 'print if /unique/;' will for instance find lines
        such as
        # tests for duplicates, to get unique elements


        The above suggestions by no means are an exhaustive list.
      • C.Y./J.E. Cripps
        ... I was actually using: $ perl -lane print if /unique/; My apology.
        Message 3 of 8 , May 28 8:37 PM
        • 0 Attachment
          > perl -e 'print if /unique/;' will for instance find lines
          > such as
          > # tests for duplicates, to get unique elements

          I was actually using:

          $ perl -lane 'print if /unique/;'

          My apology.
        • draktrax
          ... Thank you for the reply. The construct you presented, while helpful, is not shown in my book until after the exercise I m working on. Therefore: $c=0;
          Message 4 of 8 , May 29 1:08 AM
          • 0 Attachment
            --- In perl-beginner@yahoogroups.com, "C.Y./J.E. Cripps" <cycmn@...>
            wrote:
            >
            >
            >
            > > perl -e 'print if /unique/;' will for instance find lines
            > > such as
            > > # tests for duplicates, to get unique elements
            >
            > I was actually using:
            >
            > $ perl -lane 'print if /unique/;'
            >
            > My apology.
            >


            Thank you for the reply.
            The construct you presented, while helpful, is not
            shown in my book until after the exercise I'm working on.
            Therefore:

            $c=0;
            foreach $b ("spades", "hearts", "clubs", "diamonds") {
            foreach $a ("ace", 2..10, "jack", "queen", "king") {
            $deck[$c] = "$a of $b";
            $c++;
            }
            }

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

            How do I ensure no duplicates?
          • merlyn@stonehenge.com
            ... draktrax The construct you presented, while helpful, is not draktrax shown in my book until after the exercise I m working on. Therefore, you are asking
            Message 5 of 8 , May 29 6:24 AM
            • 0 Attachment
              >>>>> "draktrax" == draktrax <draktrax@...> writes:

              draktrax> The construct you presented, while helpful, is not
              draktrax> shown in my book until after the exercise I'm working on.

              Therefore, you are asking us to cheat with your homework.

              That's unethical. You should be ashamed.

              Ask your teacher for more understanding, not us for a crutch.

              --
              Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
              <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
              Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
              See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
            • draktrax
              ... 777 0095 ... discussion ... Since I am my own teacher and my text is Ellie Quigley s Perl by Example 4th Ed.; and given the fact that I have pored over the
              Message 6 of 8 , May 29 11:04 AM
              • 0 Attachment
                --- In perl-beginner@yahoogroups.com, merlyn@... wrote:
                >
                > >>>>> "draktrax" == draktrax <draktrax@...> writes:
                >
                > draktrax> The construct you presented, while helpful, is not
                > draktrax> shown in my book until after the exercise I'm working on.
                >
                > Therefore, you are asking us to cheat with your homework.
                >
                > That's unethical. You should be ashamed.
                >
                > Ask your teacher for more understanding, not us for a crutch.
                >
                > --
                > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503
                777 0095
                > <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                > Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                > See http://methodsandmessages.vox.com/ for Smalltalk and Seaside
                discussion
                >

                Since I am my own teacher and my text is Ellie Quigley's Perl by
                Example 4th Ed.; and given the fact that I have pored over the first
                seven chapters to find a solution; I must be a brick wall.
                Not only do I not see the solution,I thought I had joined a BEGINNING
                Perl forum. Instead, I get patronization and condecending remarks.
                And code snippets that are FAR beyond what I would call beginner.
                If this is the way beginners are treated, then remove my name, please.

                Thank You, Donald R. Korrecta
              • merlyn@stonehenge.com
                ... draktrax Since I am my own teacher and my text is Ellie Quigley s Perl by draktrax Example 4th Ed.; You didn t do anything to disclose that, and if you
                Message 7 of 8 , May 29 11:34 AM
                • 0 Attachment
                  >>>>> "draktrax" == draktrax <draktrax@...> writes:

                  draktrax> Since I am my own teacher and my text is Ellie Quigley's Perl by
                  draktrax> Example 4th Ed.;

                  You didn't do anything to disclose that, and if you look at the history of
                  this list, you will find that the majority of people talking about exercises
                  are in fact would-be cheaters.

                  I apologize for miscategorizing you, as you are the exception, not the rule.

                  However, if someone answers your question with constructs you haven't yet
                  seen, it'd be better to learn what those constructs are, rather than say "no,
                  that can't be the answer". Your learning will progress quicker.

                  --
                  Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                  <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                  Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                  See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
                • C.Y./J.E. Cripps
                  ... which of course needs a file name as an argument If one suspects what one wants is in the chap007.pl file $ perl -lane print if /MYTAG/; chap007.pl To
                  Message 8 of 8 , May 29 3:00 PM
                  • 0 Attachment
                    cycjec>> I was actually using:

                    >> $ perl -lane 'print if /unique/;'

                    which of course needs a file name as an argument

                    If one suspects what one wants is in the chap007.pl file

                    $ perl -lane 'print if /MYTAG/;' chap007.pl

                    To check several files with similar names

                    $ perl -lane 'print if /MYTAG/;' chap00*

                    And a solitary star * will check all files in the current directory.
                  Your message has been successfully submitted and would be delivered to recipients shortly.