Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

My instructor has asked me to use the pop, shift, and push functions to write a script that sufficiently "shuffles" a simulated deck of cards before printing the top five cards.

I came up with the following code but the problem I am having is that I can't seem to shuffle by just using pop and shift. I also don't know how to print the top 5 cards

Can anyone help me?

#!/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");
#!/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");
push(@startingdeck, shift(@startingdeck));
push(@startingdeck, pop(@startingdeck));
foreach my $card (@startingdeck) { print "Card: $card\n"}


made some more changes and i

made some more changes and i can get top 5, but no output...

my $top5 = 0;

foreach $card (@mydeck){
push (@top5, shift(@mydeck));
push (@top5, pop(@mydeck));
}
while ($top5 <= 4) {
print "$card\n";
$top5++;
}

./obj10.pl | wc
5 0 5

Hey aplata, did you ever get

Hey aplata, did you ever get this to work? I'm taking the same class and I'm able to shuffle the deck, but not getting to print out the top 5...it prints 6 instead. here's my code...

#!/usr/bin/perl -w

@mydeck = ("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");
@top5 = ();
my $x = 0;
while ($x <= 2){
push (@top5, shift(@mydeck));
push (@top5, pop(@mydeck));
$x++;
}
foreach $card (@top5){
print "$card\n";
}

did you guys resolved this issue?

hi - has anyone resolved this problem? I would like to know this correct result...Please let me know,

Thanks