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


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Need help with a Perl Script using Pop, Shift, & Push
# 1  
Old 09-28-2012
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! Smilie

Thank you all for your help.

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

Code:
#!/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:

Code:
#!/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.

Code:
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 "http://www.oreillyschool.com/certificates/sysadmin.php"
Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.
# 2  
Old 09-28-2012
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-28-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to Push Files

Hey Guys, Thanks for always being helpful, I have another issue that I need a little insight on how to fix. See the below script I have and the error I get. I don't understand why it does that, am I not using the continue correctly? #!/bin/bash -x # @(#) File: filepush.sh #... (5 Replies)
Discussion started by: gkelly1117
5 Replies

2. Web Development

Pop up confirmation box / perl cgi

Hi, I need to add confirmation pop up msg box before deleting the record from database, I have added following snippets to my code but its not working for me, your help will be much appreciated : print header; print <<EOF; <script type="text/javascript"> function confirmOk() { return... (0 Replies)
Discussion started by: terrykhatri531
0 Replies

3. Shell Programming and Scripting

Help on a script to push files once a day

Hello! Please I need some help on writing a script to push files from one server to the other once a day, everyday. I know that I can use this script on a crontab to send the files , but I am not sure how to start writing it, the actual script. I could start by declaring some variables: ... (4 Replies)
Discussion started by: fretagi
4 Replies

4. UNIX for Dummies Questions & Answers

Setup a push script from NIS

Hi guys, can we get a pushed script for every NIS client like a policy.? I need to set a symbolic links for for every client. Thanks ... (1 Reply)
Discussion started by: pianz
1 Replies

5. Shell Programming and Scripting

push and pop directories and subdirectories

I need to use pushd and popd to navigate all of the subdirectories in my current directory. I know how to get into each subdirectory, add it to the stack, and pop back out, but i cant figure out how to get into subdirectories deeper than the first without adding a foreach and if statement for... (1 Reply)
Discussion started by: ollie88r
1 Replies

6. Shell Programming and Scripting

PERL, push to hash of array problem

$key = "a"; $value = "hello"; %myhash = {} ; push @{ myHash{$key} }, $hello; print $myHash{$key}."\n"; this script prints "hello" but has following error message. Reference found where even-sized list expected at ./test line 5. can any one help me to fix this problem?? (3 Replies)
Discussion started by: bonosungho
3 Replies

7. Shell Programming and Scripting

Shift Question (Perl)

I am attempting to write a script that reads each line of a file into a separate array and does some work on it then puts it all back together and I think I need to use the 'shift()' command to read each line into its own array, but I need help nesting it into a while loop (while not eof) So... (10 Replies)
Discussion started by: ifeatu
10 Replies

8. Shell Programming and Scripting

Perl - if conditions is meet, push the last field of $_ into an array

I am using a seed file shown below to separate cisco devices by ios/os type. I want to bunch all the devices based on ios/os version. Once I find a match, I only want to push the ip address into the appropriate array. Example of seedfile 8 host1 (C3500XL-C3H2S-M) 11.0(5)WC17 10.1.44.21 9... (1 Reply)
Discussion started by: popeye
1 Replies

9. Shell Programming and Scripting

search & replace password perl script

I wanted a perl script to be done for Password search & replace in two files. For Example: Example 1)--i am having a file such as cat /opt/customer/Ariba/UAT/ariba/app/buyer/Server/config/Parameters.table Example 2)--and i am having a other file in other location such as cat... (4 Replies)
Discussion started by: shellscript22
4 Replies

10. Shell Programming and Scripting

shift and push question in perl

hi, another perl question, I don't understand the below while (<FILE>) { push @last5, $_; #add to the end shift @last5 if @last5 > 5 ; #take from the beginning } can someone please explain to me how does shift @last5 if @last5 > 5 is taking last 5 lines from... (5 Replies)
Discussion started by: hankooknara
5 Replies
Login or Register to Ask a Question