Page 1 of 1

replacing mathematical operators with a variable

Posted: Mon Mar 09, 2020 1:05 pm
by glenn9
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

Re: replacing mathematical operators with a variable

Posted: Mon Mar 09, 2020 1:53 pm
by Klaus
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

Re: replacing mathematical operators with a variable

Posted: Mon Mar 09, 2020 2:09 pm
by dunbarx
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

Re: replacing mathematical operators with a variable

Posted: Mon Mar 09, 2020 2:47 pm
by glenn9
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

Re: replacing mathematical operators with a variable

Posted: Mon Mar 09, 2020 3:11 pm
by dunbarx
Cool.

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

Craig

Re: replacing mathematical operators with a variable

Posted: Mon Mar 09, 2020 4:12 pm
by glenn9
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...

Re: replacing mathematical operators with a variable

Posted: Mon Mar 09, 2020 4:44 pm
by dunbarx
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

Re: replacing mathematical operators with a variable

Posted: Mon Mar 09, 2020 10:59 pm
by bogs
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)