simple stupid math help

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
biffbaxter
Posts: 4
Joined: Sat Feb 07, 2009 7:37 am

simple stupid math help

Post by biffbaxter » Sat Feb 07, 2009 7:43 am

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

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Feb 07, 2009 9:36 am

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
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

Garrett
Posts: 386
Joined: Sat Apr 08, 2006 8:15 am
Contact:

Post by Garrett » Sat Feb 07, 2009 10:02 am

In the script for your button put

Code: Select all

on mouseup
  put field "field 1" + field "field 2" into field "field 3"
end mouseup
But of course use the right names of your fields.

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Sat Feb 07, 2009 12:00 pm

Hi Garrett, with respect, Rev doesn't go for that...

Hi Biff
simple stupid math help
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!

• 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
• Now in your button script, you can put:

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
Hope that's helped.

P.S Welcome to the Forum!

:)

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Feb 07, 2009 12:33 pm

gyroscope,

You really don't need globals for this!!!

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

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Sat Feb 07, 2009 1:11 pm

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 :wink:).

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
and in the button script:

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
:)

SparkOut
Posts: 2944
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Sat Feb 07, 2009 2:53 pm

gyroscope wrote:Hi Garrett, with respect, Rev doesn't go for that...
Doesn't it?

Code: Select all

on mouseup
  put field "field 1" + field "field 2" into field "field 3"
end mouseup
works fine for me.
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
so as to ensure that the values are parsed before being added, but there should still be some error trapping for numbers only values to be used for this addition.
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
and in the button script

Code: Select all

on mouseUp
  put fnAddFields ("field 1","field 2") into field "field 3"
end mouseUp
And that's another of the beauties of Rev - you can find many, many, many ways to skin your cat.

[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.

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Sat Feb 07, 2009 3:43 pm

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

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!)
:)

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Feb 07, 2009 4:00 pm

Hi gyroscope,

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
or if you need a little more flexibility

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
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

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Sat Feb 07, 2009 6:26 pm

Thank you, Mark.

:)

biffbaxter
Posts: 4
Joined: Sat Feb 07, 2009 7:37 am

awesome!

Post by biffbaxter » Sat Feb 07, 2009 7:25 pm

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

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Feb 07, 2009 7:34 pm

Biff, next time, please make sure to post your script.

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

Post Reply