Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Character positioning

by Anonymous Monk
on Nov 25, 2002 at 06:50 UTC ( [id://215572]=perlquestion: print w/replies, xml ) Need Help??

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

First off, while this is a question related to a homework assignment, I certainly don't want anyone doing my homework for me. Mostly, what I'm looking for is some idea as to a reasonable path to my goal, with maybe some warnings as to what to watch out for along the way. If someone want's to give me some actual code, keen, but that's not my primary goal.

I'm working on a CGI hangman program, using the CGI.PM module to handle the CGI stuff. What I'm stuck on right now is the subroutine to reveal the word, called (imaginatively enough) reveal_word.

My data structure looks like this:

%current = ( 'word' => "", 'tries_left' => "", 'letters' => "", 'revealed' => "", );
I start by setting revealed to be a number of _ equal to how many letters are in word. Then a user makes a guess, I check and see if it's in the word, and if it is, I want to have revealed end up getting the guess (one letter) into the right "slot", with the rest of them still being _. So a guess of c with the word being cat should give me c__.

The problem is, I've no idea how to do this. It's not adding the guessed letter, it's adding it in the right place I can't figure out. How can I put that letter where I want it? Is there some clever thing involving s/// I should be trying? Or should I be completely rebuilding what goes into $current{'revealed'}? Or is there some perl command that'll just do it for me that I just can't find anywhere in my friendly O'Reilly books or over on CPAN?

Thanks, ~Pax

update (broquaint): added some formatting and changed the title (was Lost...)

Replies are listed 'Best First'.
Re: Lost...
by djantzen (Priest) on Nov 25, 2002 at 07:12 UTC

    Okay, here's an approach using substr to get you started:

    my $word = 'cat'; my $mask = '___'; my $guess = 'c'; substr($mask, 0,1) = $guess; print $mask;

    You'll still need to figure out the indexes at which the characters need to be replaced, but that shouldn't be too hard.

Re: Lost...
by Chmrr (Vicar) on Nov 25, 2002 at 07:22 UTC

    Here's another way of looking at your problem that may suggest to you a possible solution; you've got a bunch of characters in a string -- the word. For each letter, either it's a letter that's been guessed, or it isn't. If it has been guessed, just show the letter. Otherwise, show an underscore. You may find that treating the word as an array this way is useful.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Lost...
by japhy (Canon) on Nov 25, 2002 at 07:43 UTC
    I would use the following method:
    while ($current{word} =~ /$letter/g) { substr $current{revealed}, $-[0], 1, $letter; }
    This uses the @- array which holds the offsets of the entire match and the $1 (etc.) variables. Basically, $-[0] gives me the offset in the string we matched against where the pattern started. Since the real word and the revealed word are "identical", the offsets are the same.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Character Positioning
by tadman (Prior) on Nov 25, 2002 at 07:48 UTC
    Another trick is to use index. A quick tour of the Perl functions will likely give you a whole bunch of ideas. It's worthwhile reading, since it will build your understanding of what tools you have at your disposal.
Re: Character positioning
by mce (Curate) on Nov 25, 2002 at 12:09 UTC
    Hi,

    All these approches are fine, but one seems to forget that a word can contain the character multiple times. I would follow an entire new direction.

    my $word="cacao"; my @word=split ('',$word); my @string=('_') x scalar @word; my $char="c"; map { $string[$_] = $char if $word[$_] eq $char } 0..$#word; print @string;
    prints
    c_c__

    So, instead of working with strings, I would work with arrays. I even think this is faster than multiple substr lookups, but I am not sure.
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium
Re: Character positioning
by UnderMine (Friar) on Nov 25, 2002 at 13:19 UTC
    If you wish to replace all the charecters that you have not guessed with _ then :-
    s/[^$guesses]/_/g;
    Hope it helps
    UnderMine

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://215572]
Approved by mr2
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-12-03 04:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found