Page 1 of 1
difference between number variables
Posted: Wed Apr 23, 2014 2:45 pm
by coenzyme
I am trying to find the difference between 2 variables.
Say I have one variable which has a value of 10 and a second variable that has a value of 12.
The code:
does not work.
I just need variable2 or a third variable to contain the difference. In the above case I would like a variable containing 2.
Is there a function that compares two variable and returns the difference?
Re: difference between number variables
Posted: Wed Apr 23, 2014 2:50 pm
by Dixie
Code: Select all
on mouseUp
put 10 into var1
put 12 into var2
put var2 - var1 into theanswer
put theanswer
end mouseUp
Re: difference between number variables
Posted: Wed Apr 23, 2014 2:51 pm
by Klaus
Hi coenzyme,
"does not work" ERROR: Invalid error description!
Hm, just made a test with a button:
Code: Select all
on mouseUp
put 10 into variable1
put 12 into variable2
Subtract variable1 from variable2
put variable2
end mouseUp
gives me 2 in the message box, so this definitively works!
Could you please post the rest of your script(s)?
Best
Klaus
Re: difference between number variables
Posted: Wed Apr 23, 2014 2:52 pm
by coenzyme
Easy enough. Thanks!!
Re: difference between number variables
Posted: Wed Apr 23, 2014 2:56 pm
by magice
I love writing functions. This will give you the difference regardless of which variable is higher.
Code: Select all
function compareVariables x y
If x>y
then
put x-y into tResult
else
put y-x into tResult
end if
return tResult
end CompareVariables
Re: difference between number variables
Posted: Wed Apr 23, 2014 2:59 pm
by coenzyme
I am comparing two times
so when the card opens I have the code
Code: Select all
On cardOpen
get the seconds
put it into tStart
end cardOpen
I then have a button with the code in that card:
Code: Select all
On mouseUp
get the seconds
put it into tClick
Subtract tClick from tStart
End mouseUp
I am trying to find the time elapsed between the card opening and when the user clicks the button.
I am not with my code and I don't remember the error, but I can look it up when I get home.
Re: difference between number variables
Posted: Wed Apr 23, 2014 3:02 pm
by magice
I think your problem is variable persistence. Look up global and local in the dictionary.
Re: difference between number variables
Posted: Wed Apr 23, 2014 3:13 pm
by Klaus
AHA!
What magice says!
Do this in the card script:
Code: Select all
global tStart
## Actually it is: OPENCARD
On opencard
## No need to GET anything, you can put everything directly into a variable!
put the seconds into tStart
end opencard
and this in your button(s):
Code: Select all
global tStart
On mouseUp
put the seconds into tClick
Subtract tClick from tStart
End mouseUp
If you do not define a variable as either LOCAL or GLOBAL, it will be gone after your handler has finished.
Best
Klaus
Re: difference between number variables
Posted: Wed Apr 23, 2014 3:50 pm
by dunbarx
Magice,
You can also use your handler with the "abs" function, just to make it a bit shorter:
Code: Select all
function compareVariables x y
return abs(x-y)
end CompareVariables
Re: difference between number variables
Posted: Wed Apr 23, 2014 3:53 pm
by magice
dunbarx wrote:Magice,
You can also use your handler with the "abs" function, just to make it a bit shorter:
Code: Select all
function compareVariables x y
return abs(x-y)
end CompareVariables
You just took all the fun out of it

Re: difference between number variables
Posted: Wed Apr 23, 2014 4:12 pm
by Klaus
You have a real strange idea of fun!

Re: difference between number variables
Posted: Wed Apr 23, 2014 4:17 pm
by [-hh]
..........
Re: difference between number variables
Posted: Wed Apr 23, 2014 4:19 pm
by dunbarx
Klaus.
I completely get what Magice meant. Jacque has been doing this sort of thing to me for 25 years.
Craig
Re: difference between number variables
Posted: Wed Apr 23, 2014 5:16 pm
by magice
Klaus wrote:You have a real strange idea of fun!

Sometimes it's fun to reinvent the wheel, just like an artist copying the works of the masters or a musician covering someone else song. Then again, I think a clock with the gears on the outside is more fun than one with them on the inside, and I like hot rods that leave the hood off so you can see the engine, so maybe I am just strange.
Re: difference between number variables
Posted: Thu Apr 24, 2014 4:26 pm
by jacque
dunbarx wrote:Klaus.
I completely get what Magice meant. Jacque has been doing this sort of thing to me for 25 years.
Craig
Only because you're such a good sport about it. May we have 25 more.
