Comparing two variables that contain string data

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sr98bj
Posts: 3
Joined: Sat May 26, 2012 5:06 am

Comparing two variables that contain string data

Post by sr98bj » Sat May 26, 2012 5:10 am

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!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Comparing two variables that contain string data

Post by Dixie » Sat May 26, 2012 6:29 am

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 ?

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 157
Joined: Thu Jun 29, 2006 4:16 pm

Re: Comparing two variables that contain string data

Post by Randy Hengst » Sat May 26, 2012 2:42 pm

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

sr98bj
Posts: 3
Joined: Sat May 26, 2012 5:06 am

Re: Comparing two variables that contain string data

Post by sr98bj » Sun May 27, 2012 3:19 am

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!

dochawk
Posts: 43
Joined: Wed Sep 02, 2009 9:29 pm

Re: Comparing two variables that contain string data

Post by dochawk » Fri Jun 01, 2012 3:17 pm

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..

Post Reply