Page 1 of 1

Comparing two variables that contain string data

Posted: Sat May 26, 2012 5:10 am
by sr98bj
Hello Everyone, I working on my first LiveCode game and I cannot figure out why a simple if block is not working. Here is the code:

on mouseUp
put getCorrectAnswer() into theAnswer
put getOptionOne() into btnName
if btnName = theAnswer then
set the visible of button "Disabled" to true
set the label of button "Disabled" to "CORRECT"
else
set the visible of button "Disabled" to false
end if
end mouseUp

As I debug through, I can see that "theAnswer" and "btnName" both have the correct value and the equality check should pass but it fails and falls
into my else block?? Help, I confused, what am I doing wrong?

Thanks!

Re: Comparing two variables that contain string data

Posted: Sat May 26, 2012 6:29 am
by Dixie
Just a quick thought... The answers that you are comparing may look the same but it might be an idea to check the number of characters returned by each of your functions... is there a trailing space in one of them ?

Re: Comparing two variables that contain string data

Posted: Sat May 26, 2012 2:42 pm
by Randy Hengst
I'd guess that your functions are the problem... put all of this code in one button works...

function getCorrectAnswer
return "3"
end getCorrectAnswer

function getOptionOne
return random(3)
end getOptionOne

on mouseUp
local theAnswer
local btnName

put getCorrectAnswer() into theAnswer
put getOptionOne() into btnName
if btnName = theAnswer then
set the visible of button "Disabled" to true
set the label of button "Disabled" to "CORRECT"
else
set the label of button "Disabled" to "False"
-- set the visible of button "Disabled" to false
end if
end mouseUp

be well,
randy

Re: Comparing two variables that contain string data

Posted: Sun May 27, 2012 3:19 am
by sr98bj
Thank you Guys, you were exactly right. I am reading data from a SqlLite database so there were misc spaces at the beginning and/or ends of the data so the equality check was failing. I found this code:

put word 1 to -1 of bNTemp into btnName

via a Google search and it gets rid of whitespace/blanks at the beginning or ends of words/phrases and now the equality checks are working fine. Seems like this would be a common thing to do. I'm a Java programmer so is there a way in LiveCode to remove whitespace like this by calling some command or something without having to copy this code everywhere? I guess I could create a function and just pass in the phrases as parameters/input into the functions myself but wanted to know if there is an easier way?

Thanks!

Re: Comparing two variables that contain string data

Posted: Fri Jun 01, 2012 3:17 pm
by dochawk
try using regexps--pretend and append a "\ *" to the match string to match"0 or more" leading and trailing spaces.

Or

Code: Select all

If char 1 of string =spacey then put char 2 to iq of string into sting
and similarly for the end of the string..