Hash

W

weberw

How do I make the current letters appear in this version of hangman?


$current{word} = $cat

When the user enters c. I need it show c_ _.

sub processguess{


#hash of letters guessed
$current{letters} .= param('guess');
@guessed_letters = split('', $current{letters});
$current{letters} = join('', @guessed_letters);
return $current{'letters'};


my @word = split / /, $current{word};
$current{'revealed'} = join '',map {$current{letters}{$_} ? $_ : '_' }
$current{word} =~ /(.)/g;
return $current{'revealed'};
}
 
B

Brian McCauley

How do I make the current letters appear in this version of hangman?


$current{word} = $cat

When the user enters c. I need it show c_ _.

sub processguess{


#hash of letters guessed
$current{letters} .= param('guess');

Perl cannot read comments.

$current{letters} in the above is just a string/

Ig you want it to be a hash then ypu need to treat it as one. By the
magic of DWIM simply treating it as a hash will make it one.

$current{letters}{param('guess')}++;
@guessed_letters = split('', $current{letters});
$current{letters} = join('', @guessed_letters);

What do you think the above lines do?
return $current{'letters'};

What is ther purpose of this random statement?
my @word = split / /, $current{word};
$current{'revealed'} = join '',map {$current{letters}{$_} ? $_ : '_' }
$current{word} =~ /(.)/g;
return $current{'revealed'};
}

You should always use the most natural represtations of things unless
there is a reason not to. A temporary scalar value used within a
subroutine is a lexically scoped scalar variable, not an element of a
hash - $current{'revealed'}.

Please unless you actually enjoy pain, use strict and warnings.

If youm _do_ enjoy pain pleas do so in private, don't share it with the
rest of us.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top