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

question & learning about developing library funtions "lib.pl"

Expand Messages
  • fast.linux
    Here is a basic card shuffling program I wrote. I am sure it could be tweaked but it works. I am learning about developing perl libraries. How do I remove my
    Message 1 of 4 , Sep 1 2:00 AM
    • 0 Attachment
      Here is a basic card shuffling program I wrote. I am sure it could be
      tweaked but it works.

      I am learning about developing perl libraries. How do I remove my code
      that does the shuffling and include it as another function in a
      lib.pl. After the first "hand" is dealt, call the shuffling function
      again before dealing another, different hand.

      I figured out how to make a shuffling script but not sure how to make
      this work in a separate lib.pl script? Can some one point me in the
      right direction?
      Thanks for your time.

      #!/usr/bin/perl

      my @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
      "9 H","10 H","J H","Q H","K H",
      "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
      "9 D","10 D","J D","Q D","K D",
      "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
      "9 C","10 C","J C","Q C","K C",
      "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
      "9 S","10 S","J S","Q S","K S");

      my @right;
      my @left;
      SHUFFLE:
      unshift @left, pop @startingdeck for 1..26;
      @right = @startingdeck;
      @startingdeck = ();
      while(@left or @right){
      if (rand() < 0.5){
      @left and push @startingdeck, shift @left
      }else{
      @right and push @startingdeck, shift @right
      }
      };

      rand() < 0.9 and goto SHUFFLE;

      print "the top five cards are @startingdeck[0..4]\n";
    • Charles K. Clarkson
      ... You don t. At least, not in modern perl. We tend to use modules to hold reusable code. You can learn more about modules by reading the perl documentation.
      Message 2 of 4 , Sep 1 9:25 AM
      • 0 Attachment
        fast.linux wrote:

        : Here is a basic card shuffling program I wrote. I am sure it could be
        : tweaked but it works.
        :
        : I am learning about developing perl libraries. How do I remove my code
        : that does the shuffling and include it as another function in a
        : lib.pl.

        You don't. At least, not in modern perl. We tend to use modules to
        hold reusable code. You can learn more about modules by reading the perl
        documentation. You might also have a look at the source for the
        List::Util module. It has a shuffle routine in it.

        This is a great first effort for shuffling cards. The algorithm
        simulates a human splitting the deck into two equal piles and
        interlacing the two piles back into one deck.

        Humans have to shuffle cards because we are unable to easily select
        random cards from a deck of cards. It's more convenient to randomize the
        deck and to deal from the top than it is to randomly choose several
        cards from a deck.

        Computers are good at choosing random items from a list or an array.
        If we look at the deck of cards as an array, then choosing a random card
        is trivial. For example, to select a single card from the deck of cards
        in your program we only need this:

        my $random_card = $startingdeck[rand @startingdeck];


        If we pop that card out of the deck array, then we can randomly
        select a card from the remaining deck without fear of selecting the
        first card. Let's place the deck into an array named @deck and write an
        example of this.

        # Select a first random card
        my $first_random_card_index = rand @deck;
        printf "The first random card is:\t%s\n", $deck[$first_random_card_index];

        # Remove this card
        splice @deck, $first_random_card_index, 1;

        # Select a second random card
        my $second_random_card_index = rand @deck;
        printf "The second random card is:\t%s\n", $deck[$second_random_card_index];

        # Remove this card
        splice @deck, $second_random_card_index, 1;


        So, to print three random cards we could do this.

        #!/usr/bin/perl

        use strict;
        use warnings;

        foreach my $position ( qw(first second third) ) {
        printf "The %s random card is:\t%s\n", $position, remove_random_card();
        }

        BEGIN {
        my @deck = qw(
        2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
        2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
        2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
        2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
        );

        sub remove_random_card {

        # Select a random card
        my $random_card_index = rand @deck;

        # Remove this card
        my $random_card = splice @deck, $random_card_index, 1;

        # Return the card name
        return $random_card;
        }
        }

        __END__


        Still, many people prefer to work with a full deck of randomized
        cards. We could roll our own shuffle ...

        #!/usr/bin/perl

        use strict;
        use warnings;

        my @deck = qw(
        2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
        2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
        2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
        2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
        );

        my @shuffled_deck = @{ shuffle_deck( \@deck ) };

        my $i = 0;
        foreach my $position ( qw(first second third fourth fifth ) ) {
        printf "The %s random card is:\t%s\n", $position, $shuffled_deck[$i++];
        }

        sub shuffle_deck {

        # Work on copy of the deck
        my @deck = @{ $_[0] };

        my @shuffled_deck;

        foreach ( @deck ) {
        push @shuffled_deck, splice @deck, rand @deck, 1;
        }

        return \@shuffled_deck;
        }

        __END__

        ... Or we could re-use a (much) more efficient method from the
        List::Util module.

        #!/usr/bin/perl

        use strict;
        use warnings;

        use List::Util 'shuffle';

        my @shuffled_deck = shuffle(
        qw(
        2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
        2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
        2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
        2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
        )
        );

        my $i = 0;
        foreach my $position ( qw(first second third fourth fifth ) ) {
        printf "The %s random card is:\t%s\n", $position, $shuffled_deck[$i++];
        }


        __END__


        HTH,
        Charles K. Clarkson
        --
        Mobile Homes Specialist
        Free Market Advocate
        Web Programmer

        254 968-8328

        http://www.clarksonenergyhomes.com/wordpress/about/
      • fast.linux
        Thanks for the suggestions and thanks for the critique of my shuffling code. Your examples are very cool. I m off to tweak my code a bit more. Thanks for your
        Message 3 of 4 , Sep 1 10:51 AM
        • 0 Attachment
          Thanks for the suggestions and thanks for the critique of my shuffling
          code. Your examples are very cool. I'm off to tweak my code a bit
          more. Thanks for your help.

          --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
          <cclarkson@...> wrote:
          >
          > fast.linux wrote:
          >
          > : Here is a basic card shuffling program I wrote. I am sure it could be
          > : tweaked but it works.
          > :
          > : I am learning about developing perl libraries. How do I remove my code
          > : that does the shuffling and include it as another function in a
          > : lib.pl.
          >
          > You don't. At least, not in modern perl. We tend to use modules to
          > hold reusable code. You can learn more about modules by reading the perl
          > documentation. You might also have a look at the source for the
          > List::Util module. It has a shuffle routine in it.
          >
          > This is a great first effort for shuffling cards. The algorithm
          > simulates a human splitting the deck into two equal piles and
          > interlacing the two piles back into one deck.
          >
          > Humans have to shuffle cards because we are unable to easily select
          > random cards from a deck of cards. It's more convenient to randomize the
          > deck and to deal from the top than it is to randomly choose several
          > cards from a deck.
          >
          > Computers are good at choosing random items from a list or an array.
          > If we look at the deck of cards as an array, then choosing a random card
          > is trivial. For example, to select a single card from the deck of cards
          > in your program we only need this:
          >
          > my $random_card = $startingdeck[rand @startingdeck];
          >
          >
          > If we pop that card out of the deck array, then we can randomly
          > select a card from the remaining deck without fear of selecting the
          > first card. Let's place the deck into an array named @deck and write an
          > example of this.
          >
          > # Select a first random card
          > my $first_random_card_index = rand @deck;
          > printf "The first random card is:\t%s\n",
          $deck[$first_random_card_index];
          >
          > # Remove this card
          > splice @deck, $first_random_card_index, 1;
          >
          > # Select a second random card
          > my $second_random_card_index = rand @deck;
          > printf "The second random card is:\t%s\n",
          $deck[$second_random_card_index];
          >
          > # Remove this card
          > splice @deck, $second_random_card_index, 1;
          >
          >
          > So, to print three random cards we could do this.
          >
          > #!/usr/bin/perl
          >
          > use strict;
          > use warnings;
          >
          > foreach my $position ( qw(first second third) ) {
          > printf "The %s random card is:\t%s\n", $position,
          remove_random_card();
          > }
          >
          > BEGIN {
          > my @deck = qw(
          > 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
          > 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
          > 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
          > 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
          > );
          >
          > sub remove_random_card {
          >
          > # Select a random card
          > my $random_card_index = rand @deck;
          >
          > # Remove this card
          > my $random_card = splice @deck, $random_card_index, 1;
          >
          > # Return the card name
          > return $random_card;
          > }
          > }
          >
          > __END__
          >
          >
          > Still, many people prefer to work with a full deck of randomized
          > cards. We could roll our own shuffle ...
          >
          > #!/usr/bin/perl
          >
          > use strict;
          > use warnings;
          >
          > my @deck = qw(
          > 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
          > 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
          > 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
          > 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
          > );
          >
          > my @shuffled_deck = @{ shuffle_deck( \@deck ) };
          >
          > my $i = 0;
          > foreach my $position ( qw(first second third fourth fifth ) ) {
          > printf "The %s random card is:\t%s\n", $position,
          $shuffled_deck[$i++];
          > }
          >
          > sub shuffle_deck {
          >
          > # Work on copy of the deck
          > my @deck = @{ $_[0] };
          >
          > my @shuffled_deck;
          >
          > foreach ( @deck ) {
          > push @shuffled_deck, splice @deck, rand @deck, 1;
          > }
          >
          > return \@shuffled_deck;
          > }
          >
          > __END__
          >
          > ... Or we could re-use a (much) more efficient method from the
          > List::Util module.
          >
          > #!/usr/bin/perl
          >
          > use strict;
          > use warnings;
          >
          > use List::Util 'shuffle';
          >
          > my @shuffled_deck = shuffle(
          > qw(
          > 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
          > 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
          > 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
          > 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
          > )
          > );
          >
          > my $i = 0;
          > foreach my $position ( qw(first second third fourth fifth ) ) {
          > printf "The %s random card is:\t%s\n", $position,
          $shuffled_deck[$i++];
          > }
          >
          >
          > __END__
          >
          >
          > HTH,
          > Charles K. Clarkson
          > --
          > Mobile Homes Specialist
          > Free Market Advocate
          > Web Programmer
          >
          > 254 968-8328
          >
          > http://www.clarksonenergyhomes.com/wordpress/about/
          >
        • draktrax
          ... could be ... my code ... the perl ... select ... randomize the ... array. ... random card ... cards ... write an ... Same vein, different approach
          Message 4 of 4 , Sep 1 12:08 PM
          • 0 Attachment
            --- In perl-beginner@yahoogroups.com, "fast.linux" <fast.linux@...> wrote:
            >
            > Thanks for the suggestions and thanks for the critique of my shuffling
            > code. Your examples are very cool. I'm off to tweak my code a bit
            > more. Thanks for your help.
            >
            > --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
            > <cclarkson@> wrote:
            > >
            > > fast.linux wrote:
            > >
            > > : Here is a basic card shuffling program I wrote. I am sure it
            could be
            > > : tweaked but it works.
            > > :
            > > : I am learning about developing perl libraries. How do I remove
            my code
            > > : that does the shuffling and include it as another function in a
            > > : lib.pl.
            > >
            > > You don't. At least, not in modern perl. We tend to use modules to
            > > hold reusable code. You can learn more about modules by reading
            the perl
            > > documentation. You might also have a look at the source for the
            > > List::Util module. It has a shuffle routine in it.
            > >
            > > This is a great first effort for shuffling cards. The algorithm
            > > simulates a human splitting the deck into two equal piles and
            > > interlacing the two piles back into one deck.
            > >
            > > Humans have to shuffle cards because we are unable to easily
            select
            > > random cards from a deck of cards. It's more convenient to
            randomize the
            > > deck and to deal from the top than it is to randomly choose several
            > > cards from a deck.
            > >
            > > Computers are good at choosing random items from a list or an
            array.
            > > If we look at the deck of cards as an array, then choosing a
            random card
            > > is trivial. For example, to select a single card from the deck of
            cards
            > > in your program we only need this:
            > >
            > > my $random_card = $startingdeck[rand @startingdeck];
            > >
            > >
            > > If we pop that card out of the deck array, then we can randomly
            > > select a card from the remaining deck without fear of selecting the
            > > first card. Let's place the deck into an array named @deck and
            write an
            > > example of this.
            > >
            > > # Select a first random card
            > > my $first_random_card_index = rand @deck;
            > > printf "The first random card is:\t%s\n",
            > $deck[$first_random_card_index];
            > >
            > > # Remove this card
            > > splice @deck, $first_random_card_index, 1;
            > >
            > > # Select a second random card
            > > my $second_random_card_index = rand @deck;
            > > printf "The second random card is:\t%s\n",
            > $deck[$second_random_card_index];
            > >
            > > # Remove this card
            > > splice @deck, $second_random_card_index, 1;
            > >
            > >
            > > So, to print three random cards we could do this.
            > >
            > > #!/usr/bin/perl
            > >
            > > use strict;
            > > use warnings;
            > >
            > > foreach my $position ( qw(first second third) ) {
            > > printf "The %s random card is:\t%s\n", $position,
            > remove_random_card();
            > > }
            > >
            > > BEGIN {
            > > my @deck = qw(
            > > 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
            > > 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
            > > 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
            > > 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
            > > );
            > >
            > > sub remove_random_card {
            > >
            > > # Select a random card
            > > my $random_card_index = rand @deck;
            > >
            > > # Remove this card
            > > my $random_card = splice @deck, $random_card_index, 1;
            > >
            > > # Return the card name
            > > return $random_card;
            > > }
            > > }
            > >
            > > __END__
            > >
            > >
            > > Still, many people prefer to work with a full deck of randomized
            > > cards. We could roll our own shuffle ...
            > >
            > > #!/usr/bin/perl
            > >
            > > use strict;
            > > use warnings;
            > >
            > > my @deck = qw(
            > > 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
            > > 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
            > > 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
            > > 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
            > > );
            > >
            > > my @shuffled_deck = @{ shuffle_deck( \@deck ) };
            > >
            > > my $i = 0;
            > > foreach my $position ( qw(first second third fourth fifth ) ) {
            > > printf "The %s random card is:\t%s\n", $position,
            > $shuffled_deck[$i++];
            > > }
            > >
            > > sub shuffle_deck {
            > >
            > > # Work on copy of the deck
            > > my @deck = @{ $_[0] };
            > >
            > > my @shuffled_deck;
            > >
            > > foreach ( @deck ) {
            > > push @shuffled_deck, splice @deck, rand @deck, 1;
            > > }
            > >
            > > return \@shuffled_deck;
            > > }
            > >
            > > __END__
            > >
            > > ... Or we could re-use a (much) more efficient method from the
            > > List::Util module.
            > >
            > > #!/usr/bin/perl
            > >
            > > use strict;
            > > use warnings;
            > >
            > > use List::Util 'shuffle';
            > >
            > > my @shuffled_deck = shuffle(
            > > qw(
            > > 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
            > > 2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
            > > 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
            > > 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
            > > )
            > > );
            > >
            > > my $i = 0;
            > > foreach my $position ( qw(first second third fourth fifth ) ) {
            > > printf "The %s random card is:\t%s\n", $position,
            > $shuffled_deck[$i++];
            > > }
            > >
            > >
            > > __END__
            > >
            > >
            > > HTH,
            > > Charles K. Clarkson
            > > --
            > > Mobile Homes Specialist
            > > Free Market Advocate
            > > Web Programmer
            > >
            > > 254 968-8328
            > >
            > > http://www.clarksonenergyhomes.com/wordpress/about/
            > >
            >

            Same vein, different approach

            #!/usr/bin/perl -w

            $c = 0;
            foreach $b ("Spades", "Hearts", "Clubs", "Diamonds") {
            foreach $a ("Ace", 2..10, "Jack", "Queen", "King") {
            $deck[$c] = "$a of $b\n";
            $c++;
            }
            }

            $d = 1;
            @ran_ten = ();
            while ($d < 11) {
            $card = $deck [rand @deck];
            push (@ran_ten, $card);
            if (grep (/$card/, @ran_ten) == 2) {
            delete ($ran_ten[-1]);
            next;
            }
            $d++;
            }

            print " @ran_ten";
          Your message has been successfully submitted and would be delivered to recipients shortly.