in reply to Re: What I am missing here
in thread What I am missing here

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

Replies are listed 'Best First'.
Re^3: What I am missing here
by tobyink (Canon) on Mar 20, 2012 at 21:31 UTC

    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'
Re^3: What I am missing here
by heatblazer (Scribe) on Mar 21, 2012 at 05:17 UTC

    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???