replacing mathematical operators with a variable

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
glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

replacing mathematical operators with a variable

Post by glenn9 » Mon Mar 09, 2020 1:05 pm

Hi,

I'm trying to replace a mathematical operator (+, *, -, /) with a variable but LC doesn't seem to like it!

Not sure where I'm going wrong with the logic!

Code: Select all

local tANumber, tOperator, tAnotherNumber
-- instead of: put value(1 + 2) into field 1, I tried this
   put value(tANumber tOperator tAnotherNumber) into field 1
grateful for any advice

Kind regards,

Glenn

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

Re: replacing mathematical operators with a variable

Post by Klaus » Mon Mar 09, 2020 1:53 pm

Hi Glenn,

this will work with a tiny "detour":

Code: Select all

...
put 10 into aNum
put "+" into tOperator
put 22 into tNum2
put (aNum & tOperator & tNum2) into aVar
put value(aVar)
## -> 32
...
:D

Best

Klaus

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

Re: replacing mathematical operators with a variable

Post by dunbarx » Mon Mar 09, 2020 2:09 pm

What Klaus said.

The "value" function evaluates an entire string as a package.

Tbe only other way I can see doing this (rather odd) thing is:

Code: Select all

on mouseUp
   put "+" into tPlus
   do "answer 42" && tPlus && "42"
end mouseUp
Sort of the same thing, LC being asked to perform an additional level of evaluation on a prepared string.

Craig

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: replacing mathematical operators with a variable

Post by glenn9 » Mon Mar 09, 2020 2:47 pm

Klaus, Craig,

Brilliant, it works - thanks!

(the background is that I'm trying to teach my daughter some LC through building a mental arithmetic training app, and wanted to be able to change the operator through a comboBox choice, so had to have the operator as a variable!)

Thanks again,

Regards,

Glenn

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

Re: replacing mathematical operators with a variable

Post by dunbarx » Mon Mar 09, 2020 3:11 pm

Cool.

But isn't that a rather advanced concept for a newbie LC student? Unless she is in fact rather well along.... :wink:

Craig

glenn9
Posts: 234
Joined: Wed Jan 15, 2020 10:45 pm

Re: replacing mathematical operators with a variable

Post by glenn9 » Mon Mar 09, 2020 4:12 pm

We've done simple comboboxes & variables before... and I thought just swapping an operator for a variable would easily work.....!

having said that I'm still finding LC so much more enjoyable and understandable as a hobby coder than even python...

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

Re: replacing mathematical operators with a variable

Post by dunbarx » Mon Mar 09, 2020 4:44 pm

Task her to build an app. Any kind of app, an address book or something that converts millimeters to tons. I often say that it does not matter how trivial the app seems, the exercise itself will not be.

Craig

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: replacing mathematical operators with a variable

Post by bogs » Mon Mar 09, 2020 10:59 pm

dunbarx wrote:
Mon Mar 09, 2020 2:09 pm
Tbe only other way I can see doing this (rather odd) thing is:

Code: Select all

on mouseUp
   put "+" into tPlus
   do "answer 42" && tPlus && "42"
end mouseUp
Craig
:shock:

Code: Select all

put 1 into tANumber; put 5 into tAnotherNumber; put "+" into tOperator
put value(tANumber & tOperator & TAnotherNumber)
Image

Post Reply