×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

tr/// function with variable

tr/// function with variable

tr/// function with variable

(OP)
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.


CODE --> 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";
$sub =~ tr/$alphabet/\-/;
print "$sub\n"; 

RE: tr/// function with variable

(OP)
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.


CODE --> 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"; 

RE: tr/// function with variable

Hi

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

If you want a complete solution then better forget tr///, it was not invented for what you are using it. Use s/// instead :

CODE --> Perl

$Phrase = "I'm trying to find this phrase.";
$guess = "cba";
print "I already guessed these letters: $guess\n";
$sub=$Phrase;
$alphabet='abcdefghijklmnopqrstuvwxyz';
$alphabet =~ s/[\Q$guess\E]//gi;
print "These letters should still be hidden: $alphabet\n";
$sub =~ s/[\Q$alphabet\E]/-/gi;
print "$sub\n"; 

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

Feherke.
http://feherke.github.com/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close