I'm learning perl, been reading some pdfs and such which doesnt really help sink the info. So i decided to do what I did to help me with python, build something of a poker game. The cards are successfully dealt, now im working on splitting up the card terms to analyze what they're worth and alot scores.

#!/usr/bin/perl -w
use strict;

our @cards = ("Ace of Spades","Two of Spades","Three of Spades,"Four of Spades,"
           Five of Spades","Six of Spades","Seven of Spades","Eight of Spades", 
           "Nine of Spades","Ten of Spades");
           #I have the rest of the cards in this array, but 10 is enough for testing

sub fisher_yates_shuffle {
    my $deck = shift;
    my $i;
    for ($i = @@$deck; --$i ){
        my $j = int rand ($i+1);
        next if $i == $j;
        @$deck[$i,$j] = @$deck[$j,$i];
    }
    deal();
}

sub deal {
    my @ply1hand = 0;
    my @ply2hand = 0;
    
    print "Player 1s hand:\n";
    for (my $dealcount = 0; $dealcount <= 4; $dealcount++) {
         $ply1hand[$dealcount] = pop(@cards);
         print $ply1hand[$dealcount], "\n";
    }
    print "\nPlayer 2s hand:\n";
    for (my $dealcount = 0; $dealcount <= 4; $dealcount++) {
         $ply2hand[$dealcount] = pop(@cards);
         print $ply2hand[$dealcount], "\n";
    }
    print @ply1hand;         
    calc(\@ply1hand);
    calc(\@ply2hand);
}

sub calc {

    my $card1 = $_[0];
    my $card2 = $_[1];
    my $card3 = $_[2];
    my $card4 = $_[3];
    my $card5 = $_[4];
    print "card1: $card1";   
#This prints the memory address and not the single card I want. I've played around with this function quite a bit and cant get it to print a card. The idea is to split the card at white space so i can reference the face value and the suit.

Recommended Answers

All 3 Replies

I figured it out. The book im using is from 1997 haha. I was googling and the chapter on functions with arguments came from 2001 version.

So i changed

calc(\@ply1hand)

to

&calc(@ply1hand)

You mixing and matching scalars and arrays and references. Here is a scaled down version of your code that works fine.

#!/usr/bin/perl -w
use strict;
use warnings;

my @ply1hand=("Ace of Spades","Two of Spades","Three of Spades","Four of Spades","Five of Spades");
my @ply2hand=("Six of Spades","Seven of Spades","Eight of Spades", "Nine of Spades","Ten of Spades");
calc(\@ply1hand);
calc(\@ply2hand);

 
sub calc {
    my($cards)=shift; #the ref is a scalar
    print "card1: $$cards[0]\n";  #def ref the scalar for a particular array element 
    for (@$cards){ #def ref in array context for the entire array
    	print "card: $_\n";
    } 
}

1;

Well, you CAN pass an array reference. What you're doing here is passing the array itself. Both work fine. See my code for the reference version.

I figured it out. The book im using is from 1997 haha. I was googling and the chapter on functions with arguments came from 2001 version.

So i changed

calc(\@ply1hand)

to

&calc(@ply1hand)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.