CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Comparing string variables doesn't work

 



senca
Novice

Mar 13, 2011, 6:50 AM

Post #1 of 14 (1703 views)
Comparing string variables doesn't work Can't Post

hey, I'm making a 'hangman' game but for some reason when I try to compare an input character to each character in the word array, it never equals :s (I get all 'nope' e.a. . This is the code:


Code
#!/usr/bin/perl 

@words=qw(karpervissen geitlopen boksbal zwaardvis potamotrygon kleerscheur iguanodon);
$randomelement = $words[rand @words];
$lengthofword = length($randomelement);
@chars = split(//, $randomelement);
@ghostword=length(@chars);
for($i=1; $i<=@chars; $i++){
push(@ghostword, "_ ");
}

print"\nLet's play hangman children...10 guesses and you die!\nThe number of characters is $lengthofword \nGive a letter ;)\n\n";
print "@ghostword \n";
print "@chars \n ";
$guess=<STDIN>;
print "Your choice is: $guess\n\n";

##### loop through every character of the @chars array and compare ######

foreach $letter(@chars){
if($letter eq $choice){
print "roko";
}
else{
print "nope";
}
}



Karazam
User

Mar 13, 2011, 8:45 AM

Post #2 of 14 (1702 views)
Re: [senca] Comparing string variables doesn't work [In reply to] Can't Post

Well, you are saving the guess in $guess, but try to compare with $choice. Wink
With "use warnings;" perl would have warned you that $choice only occurs once.
You also need to have a "chomp $guess;" to remove it's newline character.

Instead of the foreach loop, I would suggest


Code
if ( grep {$_ eq $guess} @chars ) { 
print "roko";
}
else {
print "nope";
}


Hope this helps. Smile


(This post was edited by Karazam on Mar 13, 2011, 8:53 AM)


senca
Novice

Mar 13, 2011, 10:08 AM

Post #3 of 14 (1699 views)
Re: [Karazam] Comparing string variables doesn't work [In reply to] Can't Post

changed it but still it gives only "nope" :).


Code
foreach $letter(@chars){ 

if ( grep {$_ eq $guess} @chars ) {
print "roko";
}
else {
print "nope";
}

}



Karazam
User

Mar 13, 2011, 12:18 PM

Post #4 of 14 (1692 views)
Re: [senca] Comparing string variables doesn't work [In reply to] Can't Post

Don't put the "if ... else" part in the foreach loop, it works on its own.


senca
Novice

Mar 14, 2011, 2:50 AM

Post #5 of 14 (1685 views)
Re: [Karazam] Comparing string variables doesn't work [In reply to] Can't Post

I'm not sure what you mean by leaving the if else. But I tried changing it to this because leaving the if and else out gave errors:


Code
foreach $letter(@chars){ 

if(grep {$_ eq $guess} @chars ){
print "roko";
}

}


But again nothing.


rovf
Veteran

Mar 14, 2011, 3:03 AM

Post #6 of 14 (1684 views)
Re: [senca] Comparing string variables doesn't work [In reply to] Can't Post

Could it be that length($guess)>1 ?

;-)


Karazam
User

Mar 14, 2011, 4:57 AM

Post #7 of 14 (1681 views)
Re: [senca] Comparing string variables doesn't work [In reply to] Can't Post

I mean like this:


Code
#!/usr/bin/perl  
use warnings;

@words=qw(karpervissen geitlopen boksbal zwaardvis potamotrygon kleerscheur iguanodon);
$randomelement = $words[rand @words];
$lengthofword = length($randomelement);
@chars = split(//, $randomelement);
@ghostword=length(@chars);
for($i=1; $i<=@chars; $i++){
push(@ghostword, "_ ");
}

print"\nLet's play hangman children...10 guesses and you die!\nThe number of characters is $lengthofword \nGive a letter ;)\n\n";
print "@ghostword \n";
print "@chars \n ";
$guess=<STDIN>;
chomp $guess;
print "Your choice is: $guess\n\n";

##### loop through every character of the @chars array and compare ######

if ( grep {$_ eq $guess} @chars ) {
print "roko";
}
else {
print "nope";
}


By the way, what is the use of the following line?


Code
@ghostword=length(@chars);



senca
Novice

Mar 14, 2011, 5:15 AM

Post #8 of 14 (1679 views)
Re: [rovf] Comparing string variables doesn't work [In reply to] Can't Post

The length of $guess is always a character.

The @ghostword is the lengte of the word you need to guess but this one exists of only _ _ _ ... and after a guess the input char is being compared to every char of the word you need to guess. When it is equal, the places where it matches, should show the char in the @ghostword array. For
hidden word= blabla
input: a=l
@ghostword = _l__l_

The new if else without foreach only compares the first character and then stops apparently. And it give a "nope" even if the characters match.


(This post was edited by senca on Mar 14, 2011, 5:19 AM)


Karazam
User

Mar 14, 2011, 5:28 AM

Post #9 of 14 (1677 views)
Re: [senca] Comparing string variables doesn't work [In reply to] Can't Post

Did you try the code exactly as I posted it? Because it works for me:


Code
C:\Documents and Settings\patnym\My Documents>perl hangman.pl 
Let's play hangman children ... 10 guesses and you die!
The number of characters is 9.
Give a letter ;)

1 _ _ _ _ _ _ _ _ _
g e i t l o p e n
o
Your choice is: o

roko
C:\Documents and Settings\patnym\My Documents>


Do you have "chomp $guess;" in your code?

EDIT:
Ah, I might have misunderstood. Maybe you want the output "nope nope roko nope nope ..." to indicate where a guess matched.
In that case, just use your original foreach loop instead:


Code
foreach $letter(@chars){  
if($letter eq $guess){
print "roko";
}
else{
print "nope";
}
}


Sorry about the confusion.


(This post was edited by Karazam on Mar 14, 2011, 5:36 AM)


senca
Novice

Mar 14, 2011, 5:46 AM

Post #10 of 14 (1673 views)
Re: [Karazam] Comparing string variables doesn't work [In reply to] Can't Post

Thanks! That worked! Now I can try and complete the rest of it;)!


Karazam
User

Mar 14, 2011, 5:52 AM

Post #11 of 14 (1671 views)
Re: [senca] Comparing string variables doesn't work [In reply to] Can't Post

Great! Show us when it's finished, it seems fun. Smile


senca
Novice

Mar 14, 2011, 11:23 AM

Post #12 of 14 (1665 views)
Re: [Karazam] Comparing string variables doesn't work [In reply to] Can't Post

There is only one thing very annoying! When I print out the ghostword, it always had a number in front of it :s. Why is this? I can't figure out why.


Karazam
User

Mar 14, 2011, 11:36 AM

Post #13 of 14 (1664 views)
Re: [senca] Comparing string variables doesn't work [In reply to] Can't Post

That's because of this line:


Code
@ghostword=length(@chars);


I think you can just delete it, I don't see what good it does. And besides, you can't use "length" on an array.


(This post was edited by Karazam on Mar 14, 2011, 11:40 AM)


senca
Novice

Mar 14, 2011, 11:41 AM

Post #14 of 14 (1660 views)
Re: [Karazam] Comparing string variables doesn't work [In reply to] Can't Post

I deleted it before reading this :). I found a number of strange things in the code:). But now it finally works! I just need to finetune the game :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives