bmcquill
asked on
String Substitution
I have the below bit of code and I'm printing out the top five cards
use strict
use warnings
my @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");
my $i = 0; my @randomdeck;
while ($i < 5){
$randomdeck[$i] = shift(@startingdeck); $i++;
$randomdeck[$i] = pop(@startingdeck); $i++;
}
print "The top 5 cards are: ";
$i = 0;
while ($i < 5 ){
print "$randomdeck[$i]\n "; $i++;
}
I want to substitute Hearts for H, C for Clubs and so on. I tried
doing so:
$i = 0;
while ($i < 5 ){
if $randomdeck =~ /H/ {
s/H/Hearts/
}
Unfortunately, that's not doing the substitution and I'm pretty new to perl and regex, anyone have thoughts?
print "$randomdeck[$i]\n "; $i++;
}
use strict
use warnings
my @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");
my $i = 0; my @randomdeck;
while ($i < 5){
$randomdeck[$i] = shift(@startingdeck); $i++;
$randomdeck[$i] = pop(@startingdeck); $i++;
}
print "The top 5 cards are: ";
$i = 0;
while ($i < 5 ){
print "$randomdeck[$i]\n "; $i++;
}
I want to substitute Hearts for H, C for Clubs and so on. I tried
doing so:
$i = 0;
while ($i < 5 ){
if $randomdeck =~ /H/ {
s/H/Hearts/
}
Unfortunately, that's not doing the substitution and I'm pretty new to perl and regex, anyone have thoughts?
print "$randomdeck[$i]\n "; $i++;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ozo's answer looks like a good replacement for your:
$i = 0;
while ($i < 5 ){
if $randomdeck =~ /H/ {
s/H/Hearts/
}
But there are a lot of issues with your code, including syntax errors, like missing ';' on the 'use strict' and 'use warnings' lines. I suggest you remove such basic errors before posting your code.
$i = 0;
while ($i < 5 ){
if $randomdeck =~ /H/ {
s/H/Hearts/
}
But there are a lot of issues with your code, including syntax errors, like missing ';' on the 'use strict' and 'use warnings' lines. I suggest you remove such basic errors before posting your code.
Here's a tidier way to populate your startingdeck, in case you're interested:
my @startingdeck;
for my $suit ('H', 'D', 'C', 'S')
{
for my $num (2..10, 'J', 'Q', 'K', 'A')
{ push @startingdeck, "$num $suit" }
}
But personally, I wouldn't have a space between the number and suit for each card, unless it was needed for some good reason.
my @startingdeck;
for my $suit ('H', 'D', 'C', 'S')
{
for my $num (2..10, 'J', 'Q', 'K', 'A')
{ push @startingdeck, "$num $suit" }
}
But personally, I wouldn't have a space between the number and suit for each card, unless it was needed for some good reason.
J S
in addition to the cards you are printing