in reply to Perl subroutine update

Can you explain this piece of code
$_ =~ s/C/Clubs/,s/S/Spades/, s/H/Hearts/,s/D/Diamonds/;
what do you think it does?

Replies are listed 'Best First'.
Re^2: Perl subroutine
by craziestfire73 (Initiate) on Apr 01, 2011 at 10:38 UTC

    yes that is supposed to match for example the letter C of the deck of cards and switch it with Clubs so when you printout the deck you get for example 7 Clubs instead of 7C.

      It doesn't though, because you are doing the substitution four times on each element, thus introducing a nice echo effect.

      knoppix@Microknoppix:~$ perl -Mstrict -wE ' > my @pack = > map { > my $suit = $_; > map qq{$_$suit}, qw{ J Q K A }; > } qw{ H C D S }; > my $n = 0; > while ( $n < 4 ) > { > foreach ( @pack ) > { > s/C/Clubs/, s/S/Spades/, s/H/Hearts/, s/D/Diamonds/; > } > $n ++; > } > say qq{@pack};' JHeartseartseartsearts QHeartseartseartsearts KHeartseartseartsearts A +Heartseartseartsearts JClubslubslubslubs QClubslubslubslubs KClubslub +slubslubs AClubslubslubslubs JDiamondsiamondsiamondsiamonds QDiamonds +iamondsiamondsiamonds KDiamondsiamondsiamondsiamonds ADiamondsiamonds +iamondsiamonds JSpadespadespadespades QSpadespadespadespades KSpadesp +adespadespades ASpadespadespadespades knoppix@Microknoppix:~$

      I hope this is helpful.

      Cheers,

      JohnGG