Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: What I am missing here

by druthb (Beadle)
on Mar 20, 2012 at 20:08 UTC ( #960644=note: print w/replies, xml ) Need Help??


in reply to What I am missing here

Riales is spot-on; using Data::Dumper to dump out @deck at several places will show this quickly enough.

As an aside, I spotted another issue when I was looking at this, and couldn't run it as-shown. There's some needless complexity in sub deck_maker, which I simplified to this:

#DECK MAKER sub deck_maker { my @deck = (); #the deck for returning my @types = ('spades', 'hearts', 'diamonds', 'clubs'); my @cards = ( 2,3,4,5,6,7,8,9,10,'J','Q','K','A'); for ( my $i=0; $i < 4; $i++) { for ( my $j=0; $j < 13; $j++) { my $newcard = $cards[$j] . " of " . $types[$i]; push(@deck, $newcard); } } return @deck; #return the new deck }

D Ruth Bavousett

Replies are listed 'Best First'.
Re^2: What I am missing here
by MidLifeXis (Monsignor) on Mar 20, 2012 at 20:22 UTC

    Perhaps a little bit easier using some perl idioms...

    #DECK MAKER sub deck_maker { my @deck = (); #the deck for returning my @suits = qw( spades hearts diamonds clubs ); my @cards = ( 2..10, qw(J Q K A) ); for my $suit ( @suits ) { for my $card ( @cards ) { my $newcard = "$card of $suit"; push(@deck, $newcard); } } return @deck; #return the new deck }

    --MidLifeXis

      Can go simpler...

      sub deck_maker { map { my $suit = $_; map { "$_ of $suit" } 2..10, qw(J Q K A) } qw(spades hearts diamonds clubs); }
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Yes, I got that but I want to be more explicit of what is going on... my pick_a_card could be just :

        sub pick_a_card { pop; }

        but I prefer it my way

        With List::MapMulti this can be reduced to:

        sub deck_maker { mapm { join " of ", @_ } [ 2..10, qw(J Q K A) ], [ qw(spades hearts diamonds clubs) ] }
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Yes, it is, but I prefer to really be more verbose about my code.

        Your options are interesting and different, I prefer to code in my own style in perl, after all it`s tim towdi rule :), but I still don`t get why my pick_a_card does pops 4 times the same value???

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://960644]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2020-12-05 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (63 votes). Check out past polls.

    Notices?