Parsing and working with a string

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Parsing and working with a string

Post by Simon » Sun Dec 22, 2013 6:35 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Parsing and working with a string

Post by FourthWorld » Sun Dec 22, 2013 7:33 am

How did you get the string?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Parsing and working with a string

Post by Simon » Sun Dec 22, 2013 7:48 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parsing and working with a string

Post by Klaus » Sun Dec 22, 2013 2:08 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Parsing and working with a string

Post by dunbarx » Sun Dec 22, 2013 5:01 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parsing and working with a string

Post by Klaus » Sun Dec 22, 2013 5:38 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Parsing and working with a string

Post by jacque » Sun Dec 22, 2013 7:21 pm

I'm not sure I understand the question either. As written the string already yields a boolean.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Parsing and working with a string

Post by Simon » Sun Dec 22, 2013 11:07 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

splash21
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 369
Joined: Sun Dec 19, 2010 1:10 am
Contact:

Re: Parsing and working with a string

Post by splash21 » Mon Dec 23, 2013 2:46 am

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)
LiveCode Development & Training : http://splash21.com

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Parsing and working with a string

Post by Simon » Mon Dec 23, 2013 3:13 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Parsing and working with a string

Post by Simon » Mon Dec 23, 2013 5:16 am

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
Last edited by Simon on Mon Dec 23, 2013 11:25 am, edited 1 time in total.
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

splash21
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 369
Joined: Sun Dec 19, 2010 1:10 am
Contact:

Re: Parsing and working with a string

Post by splash21 » Mon Dec 23, 2013 10:59 am

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?
LiveCode Development & Training : http://splash21.com

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parsing and working with a string

Post by Klaus » Mon Dec 23, 2013 12:43 pm

AHA! :D

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Parsing and working with a string

Post by Simon » Mon Dec 23, 2013 11:24 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

LC4iOS
Posts: 88
Joined: Tue Dec 03, 2013 8:15 pm

Re: Parsing and working with a string

Post by LC4iOS » Tue Dec 24, 2013 7:55 am

Thanks to RunRev.
Thanks to LiveCode forum members.

LiveCode v5.5.5 - iOS Android Mac Windows - 6.5 Community
27" 2012 iMac i5, MacBook Pro, MacBook Air, iPhone 5, iPhone 4
xCode 5.0.2 - iOS7 - OS X Mavericks
Paid Apple iOS Developer Program Member

Post Reply