Page 1 of 2

Parsing and working with a string

Posted: Sun Dec 22, 2013 6:35 am
by Simon
Hello Smart People,
I have a string:

Code: Select all

(NOT favoriteColor = Blue) AND ((bestSound = Laughter) OR (bestSound= Dishes clattering in a distant restaurant) OR (happyToday = NO NOT AT ALL))
It relates to questions answered stored in an array. They are not freeform but possible choices.

How do I get the Boolean answer from/to this?
I can't even figure out how to handle more or fewer conditions. (something to do with "do"?)

Thanks for any help,
Simon

Re: Parsing and working with a string

Posted: Sun Dec 22, 2013 7:33 am
by FourthWorld
How did you get the string?

Re: Parsing and working with a string

Posted: Sun Dec 22, 2013 7:48 am
by Simon
Hi Richard,
It's part of the Question array, these are preconditions to asking the next question.
hmmm... They are a separate value among other values in the key.

Does that explain it better?

Simon

Re: Parsing and working with a string

Posted: Sun Dec 22, 2013 2:08 pm
by Klaus
Hi Simon,

sorry, don't get the question?
If all required data are in variables, what is the problem with these evalutations?
They are as clear as mud! :D


Best

Klaus

Re: Parsing and working with a string

Posted: Sun Dec 22, 2013 5:01 pm
by dunbarx
Simon.

Code: Select all

bestSound= Dishes clattering in a distant restaurant
Were you remiss in putting quotes around some of this stuff? I don't think it is good practice to have a variable name consisting of six words.

Also, you have an extra paren in front of "bestSound"

comment backwards from the right side, phrase by phrase. You get errors until only the first one is clear. Then run back to the right, exposing one at a time. You can see what is wrong at each stage.

Craig

Re: Parsing and working with a string

Posted: Sun Dec 22, 2013 5:38 pm
by Klaus
Hi Simon,

supposed favoriteColor, bestSound and happyToday are variables:
...
if (favoriteColor <> "Blue")
AND
## one of the following expressions = true
(
(bestSound = "Laughter")
OR
(bestSound= "Dishes clattering in a distant restaurant")
OR
(happyToday = "NO NOT AT ALL")
)
...
THEN the complete expression yields to TRUE :D


Best

Klaus

Re: Parsing and working with a string

Posted: Sun Dec 22, 2013 7:21 pm
by jacque
I'm not sure I understand the question either. As written the string already yields a boolean.

Re: Parsing and working with a string

Posted: Sun Dec 22, 2013 11:07 pm
by Simon
Hello All,
Thank you for your replies

I didn't write and have no control over the string. This is what I get to work with and now have to get in into liveCode and make it do what it's asking for.

Now setting the itemDel to "=" I can get the key (favoriteColor bestSound happyToday), err... put the last word of item 1. But is that the best way to go about it.
Do I, put offset("(",tString) into tStartChar etc and work with
(NOT favoriteColor = Blue)

Or is there something really cool in liveCode that I've not used before?
Sorry I haven't been clear.

Thanks again,
Simon

Re: Parsing and working with a string

Posted: Mon Dec 23, 2013 2:46 am
by splash21
Hi, Simon. I've been using regex's recently, so here are some thoughts;

Code: Select all

on mouseUp
     # first add quotes to the values
   local tString, tStart, tEnd, tMatch
   put "(NOT favoriteColor = Blue) AND ((bestSound = Laughter) OR (bestSound= Dishes clattering in a distant restaurant) OR (happyToday = NO NOT AT ALL))" into tString
   repeat while matchChunk(tString, "=\W*[^" & quote & "]\b([[:alnum:]\s]+)\b[^" & quote & "]\W", tStart, tEnd)
      put char tStart to tEnd of tString into tMatch
      put quote & tMatch & quote into char tStart to tEnd of tString
   end repeat
   
   # cheeky replacement to allow LC to evaluate the NOTs correctly
   replace "(NOT" with "NOT(" in tString
   
   # show the modified string
   answer "The string is now;" & LF & tString 
   
   # try and evaluate the string
   # change these values for testing the result - the real values would come from the data
   local favoriteColor, bestSound, happyToday
   put "Orange" into favoriteColor
   put "Laughter" into bestSound
   put "Yes" into happyToday
   answer  "Expression evaluates to " & value(tString)
end mouseUp
Firstly, I've added quotes around the values - so far so good!
Then I've adjusted the 'NOT' so that it will evaluate properly
Finally, I've defined the values that I assume would be read from a database or file, etc...
(My test values evaluate to true, you can mess about with them more to test)

This could be totally off in the wrong direction, but hopefully it helps! :D


(would be a lot easier if replaceText supported backreferences)

Re: Parsing and working with a string

Posted: Mon Dec 23, 2013 3:13 am
by Simon
Hi splash21,
Thanks!

Code: Select all

value(tString)
Wicked! part of the problem solved.

I'll work with this and the regex maybe I'll get it right.

Simon

Re: Parsing and working with a string

Posted: Mon Dec 23, 2013 5:16 am
by Simon
Within the same problem

Code: Select all

   put "Orange" into favoriteColor
   put "Laughter" into bestSound
   put "Yes" into happyToday
Because this is in an array e.g.
141
...tag....favoriteColor
...Query...What is your favorite color?
...Response... Blue

How do I "put "Blue" into favoriteColor"?

Now the answer array I do have control over and maybe I have set it up incorrectly maybe I should just have
141
...favoriteColor...Blue

but again I'm stuck. There are way too many questions to have a bunch of variables laying around.
with this

Code: Select all

NOT( favoriteColor = "Blue") AND ((bestSound = "Laughter") OR (bestSound= "Dishes clattering in a distant restaurant") OR (happyToday = "NO NOT AT ALL"))
I can now evaluate it (Thanks again splash21). Just getting the values into those vars is now my problem.


Please keep asking me questions, I know I'm doing something really dumb because liveCode can do everything! :)

Simon

Re: Parsing and working with a string

Posted: Mon Dec 23, 2013 10:59 am
by splash21
I must have misunderstood - I thought the string was supplied and you had to take it apart - do you actually create the string from the array? There could be a far nicer solution - can you supply some of the data that you have to work with in it's raw form?

Re: Parsing and working with a string

Posted: Mon Dec 23, 2013 12:43 pm
by Klaus
AHA! :D

OK, I see you are on your way...

Re: Parsing and working with a string

Posted: Mon Dec 23, 2013 11:24 pm
by Simon
Hi splash21,
Here is a sample of what I'm getting in the array I have no control over:
141
...questionType...multi
...questionsRemainingInBigSection...63
...tag...favoriteColor
...precondition...(NOT sightImpared = Very) AND (colorBlind = No)
...nextQ...favoriteFood
...valuesQ...Red%@Blue%@Green%@Yellow%@Orange%@Pink%@Purple
...required
...minQ (used when a slider is made available)
...exclusiveValue
...textQ...What is your favorite color?
...idQ...141
...superseding
...inception...2013-07-11 00:00:00
...questionsRemaining...1226
...usedInRules
...sectionName...About your likes
...maxQ...(used when a slider is made available)
...bigSection...2
I only need a few of those pairs one of them being the precondition (my original post) I record answers like this:
141
...tag....favoriteColor
...Query...What is your favorite color?
...Response... Blue
Note they are both arrays and not variables, which is my current problem.
I could save out all the [tag] and make them custom properties but then the app would have to be updated if new questions came along.

Yes, I would very much like to "put "Blue" into favoriteColor"

I could pre setup 10 locals and swap out each item in the precondition but is that really the best way?

Your regex and value(tString) are still part of the solution so that time was not wasted.

Thanks again for your time,
Simon

Re: Parsing and working with a string

Posted: Tue Dec 24, 2013 7:55 am
by LC4iOS