Need help with a Perl Script using Pop, Shift, & Push

Hello everyone,

I am new to Perl and I am having some issues getting a script to work. I have to create a script that uses an array of 52 cards, "shuffles" the cards (using loops with the pop, shift, and push commands), and prints out the top five. This is not a randomizing of the array just a rearrangement of the array. I have been able to get it to rearrange the array, but not with the result that I would like. I have been working on this for a week and have been running in circles! :wall:

Thank you all for your help.

Here is the code that I have been given to start:

#!/usr/bin/perl

@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");

And here is the instructions that I have been provided to accomplish this objective:

Using pop, shift, push, and the starting code below, write a script that sufficiently "shuffles" a simulated deck of cards before printing the top five cards. Save this script in your home directory as obj10.pl. The goal of this objective is to familiarize yourself with these three functions while using loops and to use them together to rearrange the array but not a randomly shuffled array.

Here is the latest script that I have come up with:

#!/usr/bin/perl

@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");

for($i=0 ; $i < 26 ; $i++){
        $left = pop(@startingdeck);
        push(@shuffled,$left);

        $right = shift(@startingdeck);
        push(@shuffled,$right);
}

foreach $card (@shuffled){
    print "$card\n";
}

Here is the result of this script:
I am just working on the rearranging of the array right now, and still need to add the commands to print the top 5.

K S
A H
Q S
2 H
J S
3 H
10 S
4 H
9 S
5 H
8 S
...
...
...
6 C
8 D
5 C
9 D
4 C
10 D
3 C
J D
2 C
Q D
A C
K D

O'Reilly School Of Technology(University of Illinois), Urbana-Champaign, IL USA, Kelly Hoover, Linux/Unix 4: Scripting for Administrators Sed, Awk, and Perl "O'Reilly Media - Technology and Business Training"

You already know how a for-loop works, it seems; the top 5 would just be another loop, from 0 to 5.

As for shuffling the cards, how does one usually shuffle a deck? Split it in half, then combine them randomly. I might try popping each card from the deck individually and putting them in either array A or B. Then looping until array A and B are both empty, popping a card from either A or B each loop and pushing it back into the original array. You could randomly choose which array you pick from.

1 Like

Corona688,

Thank you! That has been a great help to getting the start that I needed. I had worked with splitting the original array, but then had my loops becoming way too complicated and then they weren't running correctly.