So we setup $unknown by assigning $info then replacing all alphabetic characters with asteriks. Then we loop until $info is equal to $unknown i.e when all the characters have been guessed. On every iteration the user is prompted to enter a character, which is read as a line from STDIN and the first character is used. If the given character is not in the alphabet or found in the string, then we reiterate, otherwise we loop through $info looking for $c and replacing the appropriate position in $unknown with $c, then print $unknown.use strict; my $info = "I am a brown icecube"; (my $unknown = $info) =~ tr[a-zA-Z][*]; until($info eq $unknown) { print "guess a char: "; chomp(my $input = <STDIN>); my $c = substr $input => 0, 1; next if $c !~ /[a-z]/i or index($info => $c) == -1; substr($unknown => pos($info) - 1, 1) = $c while $info =~ /$c/g; print $unknown, $/; }
For more info on the operators and functions used above see. tr, substr, s///, index and pos. If the => is a little confusing it's just the comma in disguise, aka that 'fat comma'.
HTH
_________
broquaint
In reply to Re: string matching/replacing
by broquaint
in thread string matching/replacing
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |