Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Let's say we have $info and $infounknown; $info is a word or phrase or set of numbers while $infounknown is the exact number of characters as $info but replaces the characters with ****.

My question for all the wise monks is how can you compare $info to $infounknown when one is the actual string you're looking for while the other is using a glob of asterics as placeholders? My other question is, how do you then replace all instances of characters you type in (or word) for $infounknown.

An example would be something like: $info= "I am a brown icecube" and $infounknown would be "* ** * ***** *******". When the person searches for the letter A, it would then show '* a* a ***** *******'.

Questions: how can you use one as placeholders but still verifiy the values? How can you replace the values in $infounknown if they exist?

Replies are listed 'Best First'.
Re: string matching/replacing
by broquaint (Abbot) on Sep 06, 2003 at 22:37 UTC
    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, $/; }
    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.

    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

Re: string matching/replacing
by antirice (Priest) on Sep 06, 2003 at 20:30 UTC

    One way is to build a string of characters that should be replaced and use it with tr and eval.

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      How about something like this?

      $chars will just contain a list of those you want to expose, everything else is converted to an asterisk or what ever you want. You may want to keep an original as I did with $info. Then use something like $known to expose the letters as desired. Notice, I included a space in the reveal chars to match your example.

      I hope this helps :-)

      #!/usr/bin/perl -w use strict; my $info = "I am a brown icecube"; my $chars = 'a '; my $known = $info; $known =~ s/[^$chars]/\*/g; print "$info\n"; print "$known\n";
Re: string matching/replacing
by sgifford (Prior) on Sep 07, 2003 at 04:10 UTC
    Splitting into an array simplifies this problem:
    #!/usr/bin/perl -w use strict; use vars qw($str @s @f); $str = 'I am a brown icecube'; @s = split('',$str); @f = map {$_ eq ' ' ? ' ' : '*'} @s; while (<>) { my $guess=substr($_,0,1); foreach my $i (0..$#s) { if (lc $s[$i] eq lc $guess) { $f[$i] = $s[$i] } } print @f,"\n"; }
Re: string matching/replacing
by graff (Chancellor) on Sep 07, 2003 at 03:56 UTC
    This sounds a lot like an application I wrote in my early days of learning Perl/Tk, which I posted here.

    I called it "Tk Cryptogram", because it was for doing the "cryptogram" puzzles found in various daily newspapers: the reader sees a quotation where each letter has been "encrypted" as some other random letter -- e.g. all A's have been printed as P's, and so on.

    To solve the puzzle, the user enters the "encrypted" string, and sees an equivalent string where spaces and punctuation are the same, but all letters show up as underscores; for each "encrypted" letter, the user types in a "decoded" letter, and this automatically replaces the corresponding underscores to "decrypt" the quotation.