value() function for invalid expressions

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
acwilson96
Posts: 22
Joined: Tue Nov 26, 2013 9:55 pm

value() function for invalid expressions

Post by acwilson96 » Thu Feb 13, 2014 1:33 pm

I am creating a simple maths game and I want the user to be able to type in a sum using numbers and operations and brackets but I cannot find a way of letting them use brackets without the possibility of error, for example when they enter a sum, e.g. 9+2, if they have brackets like this: (9+2( then when I use

Code: Select all

value(field userAnswerField)
it wont run as the entered input is not of correct syntax. Is there any way of having the program ignore it if it cannot be 'assigned a value'.

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

Re: value() function for invalid expressions

Post by Klaus » Thu Feb 13, 2014 1:41 pm

Hi acwilson96,

you can use a TRY structure to catch eventual errors!
Example:

Code: Select all

...
try
  ## In case of success:
  put value(field "userAnswerField") into fld "UserResult"

## catch eventual error:
catch errornum
  anser "Wrong input! Try again."
  ## or do whatever you want in this case of erratic user input.
end try
...
This will erm. TRY to put the value of the text field into another field
and answer a dialog if it does not succeed!


Best

Klaus

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

Re: value() function for invalid expressions

Post by dunbarx » Thu Feb 13, 2014 4:26 pm

Hi.

What Klaus said.

Not brackets, "[]". Parentheses. "()", right?

Another way, if you like to assemble houses of cards like I do, is to validate the string completely. You said you always have parentheses, so you can make sure that there is a single open paren, "(", and that a single close paren ")" follows it. This can be done with such tools as the offset function and perhaps the itemdelimiter. Can you do this sort of thing? If not, write back, but it would be a great exercise in fooling around with LC text and chunking.

Craig Newman

acwilson96
Posts: 22
Joined: Tue Nov 26, 2013 9:55 pm

Re: value() function for invalid expressions

Post by acwilson96 » Thu Feb 13, 2014 9:19 pm

Thanks for the replies I will try them tomorrow when I am back at my PC and yes I meant parentheses! I will look into "itemdelimiter" and see if I can get anywhere and will post back the results!

Post Reply