Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tr/// function with variable

Status
Not open for further replies.

Fafnir956

Programmer
Mar 6, 2013
2
US
I'm trying to make a hangman game and I've run into a specific issue with the tr/// function. I want to replace the letters in the phrase that have not been guessed yet. This code does not work and I know that the highlighted line is the wrong one but I can't figure out how to make it work. Any help will be very appreciated. This is not homework. I am trying to create problems for my student that I tutor. I know there are probably different ways of solving this problem, but I would like to stick with the tr/// operator because it makes the most sense to me.


Perl:
$Phrase = "I'm trying to find this phrase.";
$guess = "abcde";
print "I already guessed these letters: $guess\n";
$sub=$Phrase;
$alphabet='abcdefghijklmnopqrstuvwxyz';
$alphabet =~ s/$guess//g;
print "These letters should still be hidden: $alphabet\n";
[highlight #FCE94F]$sub =~ tr/$alphabet/\-/;[/highlight]
print "$sub\n";
 
Hi

See Quote and Quote-like Operators in [tt]man perlop[/tt]. It says [tt]tr///[/tt] ( and of course [tt]y///[/tt] ) does not interpolate.

The same manual page proposes a solution later : use eval :
Code:
[b]eval[/b] [green][i]"\$sub =~ tr/$alphabet/\-/"[/i][/green][teal];[/teal]

Feherke.
[link feherke.github.com/][/url]
 
Your code almost worked, but not exactly. It couldn't make sense of the guesses "cba", but it did do "abc". I also had a hard time getting it to be case insensitive with tr///i with the eval function, so I just made a list of the uppercase values and included them in there too. You should test your code before you post it in the future. :) Thanks for the push in the right direction though.


Perl:
$Phrase = "I'm trying to find this phrase.";
$guess = "cba";
print "I already guessed these letters: $guess\n";
$sub=$Phrase;
$alphabet='abcdefghijklmnopqrstuvwxyz';
eval"\$alphabet =~ tr/$guess//d";
print "These letters should still be hidden: $alphabet\n";
$ucalph=uc($alphabet);
eval"\$sub =~ tr/$alphabet$ucalph/\-/";
print "$sub\n";
 
Hi

I only answered what you asked : the use of [tt]tr///[/tt] with variable.

If you want a complete solution then better forget [tt]tr///[/tt], it was not invented for what you are using it. Use [tt]s///[/tt] instead :
Perl:
[navy]$Phrase[/navy] [teal]=[/teal] [green][i]"I'm trying to find this phrase."[/i][/green][teal];[/teal]
[navy]$guess[/navy] [teal]=[/teal] [green][i]"cba"[/i][/green][teal];[/teal]
[b]print[/b] [green][i]"I already guessed these letters: $guess\n"[/i][/green][teal];[/teal]
[navy]$sub[/navy][teal]=[/teal][navy]$Phrase[/navy][teal];[/teal]
[navy]$alphabet[/navy][teal]=[/teal][green][i]'abcdefghijklmnopqrstuvwxyz'[/i][/green][teal];[/teal]
[navy]$alphabet[/navy] [teal]=~[/teal] [highlight][b]s[/b][fuchsia]/[\Q$guess\E]//[/fuchsia][b]gi[/b][/highlight][teal];[/teal]
[b]print[/b] [green][i]"These letters should still be hidden: $alphabet\n"[/i][/green][teal];[/teal]
[navy]$sub[/navy] [teal]=~[/teal] [highlight][b]s[/b][fuchsia]/[\Q$alphabet\E]/-/[/fuchsia][b]gi[/b][/highlight][teal];[/teal]
[b]print[/b] [green][i]"$sub\n"[/i][/green][teal];[/teal]

BTW, unless stated otherwise, I always test the codes I post. But of course, only for the input provided in the question.


Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top