Evaluate text of field

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
doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Evaluate text of field

Post by doobox » Fri Aug 03, 2012 11:45 pm

Hi there..
Stuck on a real simple one probably.. :-)

I have a field "test" contents of the field are for example "1+2=3"

I can do this:

Code: Select all

do field "test" as applescript
put the result into field "answer"
And i get the expected "true"

BUT..!
I'd rather not do things like that if i can help it.
So is there a way to convert the text of a field from a literal string, so it can be evaluated normally...?
Kind Regards
Gary

https://www.doobox.co.uk

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Evaluate text of field

Post by sturgis » Sat Aug 04, 2012 12:10 am

look at value()

with your example if you have 1 + 2 = 3 in field 1 then executing value(field 1) in the msg box returns true.
Changing it to 1 + 2 = 4 and then executing value(field 1) returns false as expected.

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Evaluate text of field

Post by doobox » Sat Aug 04, 2012 12:22 am

Perfect.. Thank you. That's the function a new must have existed.
I wonder how expensive it is.
I will be iterating in a repeat loop, and i need that answer real fast. We will see :-)
Kind Regards
Gary

https://www.doobox.co.uk

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Evaluate text of field

Post by sturgis » Sat Aug 04, 2012 12:28 am

Don't use the field directly (for your list)
put the field contents into a variable and iterate through that.

If the list is REALLY long, use the "for each" form of repeat rather than "with i = 1 to..." form

Should be faster that way.

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

Re: Evaluate text of field

Post by dunbarx » Sat Aug 04, 2012 12:32 am

Hi.

What Sturgis said. Note that this works with text and lots of other expressions, like "a < b", 4 is an integer, "1/1/12 < 2/1/12".

All true.

And if you only had: "3 + 5", you would get "8".

Be careful, though. Try: "4 is a date"

Craig Newman

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Evaluate text of field

Post by doobox » Sat Aug 04, 2012 12:40 am

Thank's Craig...

Yep.. this can be real handy, when your sure you know what your evaluating.
Like you say though, it could quickly lead to unexpected bugs if one's not careful.

Try "4 is a char" that was pretty unexpected also :-) "Throws an ERROR..!"
Kind Regards
Gary

https://www.doobox.co.uk

Post Reply