simple stupid math help
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 4
- Joined: Sat Feb 07, 2009 7:37 am
simple stupid math help
Hi all,
I have been reading about but cant seem to get my arms around how to do some basic math, so I am hoping for an example. I will explain.
I have three text fields (two are user entered values)
The first field is numeric value the user enters
The second field is a numeric value the user enters
The third text field yields the results. (first*second)
A simple push button triggers the math.
I have been poking around the user guide and tutorials but cant seem to put something together that seems very easy.
Thanks for the help!
biff
I have been reading about but cant seem to get my arms around how to do some basic math, so I am hoping for an example. I will explain.
I have three text fields (two are user entered values)
The first field is numeric value the user enters
The second field is a numeric value the user enters
The third text field yields the results. (first*second)
A simple push button triggers the math.
I have been poking around the user guide and tutorials but cant seem to put something together that seems very easy.
Thanks for the help!
biff
Hi Biff,
Could you please post your current script? Have you read about the basics in the docs? (There is a PDF included with Revolution, which contains all the basics).
Best,
Mark
Could you please post your current script? Have you read about the basics in the docs? (There is a PDF included with Revolution, which contains all the basics).
Best,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
In the script for your button put
But of course use the right names of your fields.
Code: Select all
on mouseup
put field "field 1" + field "field 2" into field "field 3"
end mouseup
Hi Garrett, with respect, Rev doesn't go for that...
Hi Biff
• Because the two number fields change, the number are, therefor, variable. So they need to be put into variables to perform the multiplication to insert into the third (answer) field.
• If the 2 numbers were in one field, one could use a local or even a temporary variable, but because the numbers are in two fields, each number has to be put into a global variable, so that Rev "remembers" outside of the button's script.
• It took hours of headscratching when I started using Rev to try to understand a simple error message I was getting; this concerned putting code into an "on preOpenStack", or "on openStack" script. The way Rev works here is that anything put into a pre- or openStack related to all stacks, including sub Stacks. Your small app here only needs one card on one stack, but it's still useful to know that you are on safe ground putting stuff into an "on preOpenCard" or "OpenCard" handler instead until you know more about generic scripts which affect every stack.
• So firstly, in your card script, we have to "declare" global variables and ideally give them an initial value:
• Now in your button script, you can put:
Hope that's helped.
P.S Welcome to the Forum!

Hi Biff
Delete word "stupid" and you're there! Don't worry about how straightforward or simple your question is; everyone here gladly helps no matter what type of Rev question, so no worries!simple stupid math help
• Because the two number fields change, the number are, therefor, variable. So they need to be put into variables to perform the multiplication to insert into the third (answer) field.
• If the 2 numbers were in one field, one could use a local or even a temporary variable, but because the numbers are in two fields, each number has to be put into a global variable, so that Rev "remembers" outside of the button's script.
• It took hours of headscratching when I started using Rev to try to understand a simple error message I was getting; this concerned putting code into an "on preOpenStack", or "on openStack" script. The way Rev works here is that anything put into a pre- or openStack related to all stacks, including sub Stacks. Your small app here only needs one card on one stack, but it's still useful to know that you are on safe ground putting stuff into an "on preOpenCard" or "OpenCard" handler instead until you know more about generic scripts which affect every stack.
• So firstly, in your card script, we have to "declare" global variables and ideally give them an initial value:
Code: Select all
global gFig1, gFig2
on OpenCard
put 0 into gFig1
put 0 into gFig2
--and to make it a little more polished:
put 0 into field "FieldA"
put 0 into field "FieldB"
put 0 into field "FieldC"
end OpenCard
Code: Select all
global gFig1, gFig2
on mouseDown
put field "FieldA" into gFig1
put field "FieldB" into gFig2
put gFig1*gFig2 into tRes
put tRes into field "FieldC"
end mousedown
P.S Welcome to the Forum!

Hallo Mark; you are so right: in my attempt to be precise and thorough I go overboard sometimes! (Part of the beauty of the Rev language is the powerful from the simple, as you of course know, which I seem to want to overcomplicate unnecessarily sometimes
).
Biff, so the code could be:
and in the button script:


Biff, so the code could be:
Code: Select all
-- to make it a little more polished:
on OpenCard
put 0 into field "FieldA"
put 0 into field "FieldB"
put 0 into field "FieldC"
end OpenCard
Code: Select all
on mouseDown
put field "FieldA" into tFig1
put field "FieldB" into tFig2
put gFig1*gFig2 into tRes
put tRes into field "FieldC"
end mouseDown

Doesn't it?gyroscope wrote:Hi Garrett, with respect, Rev doesn't go for that...
Code: Select all
on mouseup
put field "field 1" + field "field 2" into field "field 3"
end mouseup
You would need to do some error trapping to make sure the values are numbers, so as not to get type mismatch errors, but the simple syntax should be OK for most situations I can think of. It might be worth doing something like
Code: Select all
on mouseUp
put (the text of field "Field1") + (the text of field "Field2") into field "Field3"
end mouseUp
One of the things that is so nice about Rev is that it does try its best to interpret the code to work in context. It gives a lot more freedom than a heavily typed language, but that does come with a few caveats.
My preferred way of doing this would be to make a function (put it in the card or stack script as appropriate)
Code: Select all
function fnAddFields pField1, pField2
local tVal1, tVal2
put the text of field pField1 into tVal1
put the text of field pField2 into tVal2
if tVal1 is not a number or tVal2 is not a number then
return "non numeric"
else
return tVal1 + tVal2
end if
end fnAddFields
Code: Select all
on mouseUp
put fnAddFields ("field 1","field 2") into field "field 3"
end mouseUp
[edit]OK I read "add" for "multiply" but the principle is the same![/edit]
Last edited by SparkOut on Sat Feb 07, 2009 4:06 pm, edited 1 time in total.
Hi Garrett
)
THanks for another example of a function (in my prev. post I mentioned about being wary of functions but am catching on now, I think...)
My confusion was how to "call up" a function; I see know that the returned value(s) from the function are obtained from within a separate on mouseUp handler, calling the function itself. Why I didn't get that before, I don't know.
I guess a function is a handler returning value(s) but called up by another (mouseUp/Down) handler. Is that about it or is there any other considerations when describing functions
(Biff, hope you don't mind my "changing the rails" on your thread, for the while, thanks!)

Um, it does for me now, as well...whoops again, (perhaps I'm getting tea drunkCode:
on mouseup
put field "field 1" + field "field 2" into field "field 3"
end mouseup
works fine for me.

THanks for another example of a function (in my prev. post I mentioned about being wary of functions but am catching on now, I think...)
My confusion was how to "call up" a function; I see know that the returned value(s) from the function are obtained from within a separate on mouseUp handler, calling the function itself. Why I didn't get that before, I don't know.
I guess a function is a handler returning value(s) but called up by another (mouseUp/Down) handler. Is that about it or is there any other considerations when describing functions

(Biff, hope you don't mind my "changing the rails" on your thread, for the while, thanks!)

Hi gyroscope,
My guess is that you run into problems if one of the fields is not a number. So, you might do this:
or if you need a little more flexibility
Best,
Mark
My guess is that you run into problems if one of the fields is not a number. So, you might do this:
Code: Select all
if fld "a" & fld "b" & fld "c" is a number then
put fld "a" + fld "b" + fld "c" into fld "d"
end if
Code: Select all
if fld "a" is a number and fld "b" is a number and fld "c" is a number then
put fld "a" + fld "b" + fld "c" into fld "d"
end if
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- Posts: 4
- Joined: Sat Feb 07, 2009 7:37 am
awesome!
This is great guys thanks! I appreciate the candor and feedback. This will make my life much easier as I learn the syntax.
regards,
biff
regards,
biff