subtracting two variables

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
Samuele
Posts: 282
Joined: Mon Oct 11, 2021 7:05 pm
Location: Italy

subtracting two variables

Post by Samuele » Tue Mar 15, 2022 9:10 pm

Hi how can i put two subtracted variables in a third variable? for example

Code: Select all

put (Var1 - Var2) into Var3 #(this doesn't work)

Thanks!
Samuele.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: subtracting two variables

Post by Klaus » Tue Mar 15, 2022 9:15 pm

Leave out the parenthesis:

Code: Select all

put Var1 - Var2 into Var3

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: subtracting two variables

Post by dunbarx » Tue Mar 15, 2022 9:16 pm

Samuele.

It sure should. Sure you have it exactly right? Are you sure that each variable contains ONLY a number???

Craig

andresdt
Posts: 146
Joined: Fri Aug 16, 2019 7:51 pm

Re: subtracting two variables

Post by andresdt » Tue Mar 15, 2022 9:16 pm

Samuele wrote:
Tue Mar 15, 2022 9:10 pm
Hi how can i put two subtracted variables in a third variable? for example

Code: Select all

put (Var1 - Var2) into Var3 #(this doesn't work)

Thanks!
that should work if both of your variables contain a number.

Code: Select all

if Var1 is a number and Var2 is a number then
  put Var1 - Var2 into Var3
end if

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: subtracting two variables

Post by dunbarx » Tue Mar 15, 2022 9:17 pm

Klaus.

The parens do not harm the line of code.

Craig

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: subtracting two variables

Post by Klaus » Tue Mar 15, 2022 9:19 pm

Yes, I was not sure and had to make a quick test.
True, parens do not make a difference.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: subtracting two variables

Post by dunbarx » Tue Mar 15, 2022 9:22 pm

OK.

So I am betting a dollar that the variables have non-numeric trash mixed in. Anyone?

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: subtracting two variables

Post by FourthWorld » Tue Mar 15, 2022 9:27 pm

dunbarx wrote:
Tue Mar 15, 2022 9:22 pm
So I am betting a dollar that the variables have non-numeric trash mixed in. Anyone?
Sanuele, do you have Suppress Errors turned on?

Normally an issue with addition should throw a script error telling you exactly which variable is preventing the arithmetic from happening.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Samuele
Posts: 282
Joined: Mon Oct 11, 2021 7:05 pm
Location: Italy

Re: subtracting two variables

Post by Samuele » Wed Mar 16, 2022 12:15 am

FourthWorld wrote:
Tue Mar 15, 2022 9:27 pm
dunbarx wrote:
Tue Mar 15, 2022 9:22 pm
So I am betting a dollar that the variables have non-numeric trash mixed in. Anyone?
Sanuele, do you have Suppress Errors turned on?

Normally an issue with addition should throw a script error telling you exactly which variable is preventing the arithmetic from happening.
ahh, it did throw an error that i didn't understand, but i have changed the script to

Code: Select all

put (Var1 - (Var2)) into Var3
and now it seems to be working.
Samuele.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: subtracting two variables

Post by dunbarx » Wed Mar 16, 2022 2:16 am

Samuele.

I cannot see any way that adorning with parentheses fixed the problem. In fact, as we know, you do not need any at all.

Have you tried what andresdt suggested, that is, to validate that the two variables contain only pure numbers before you try to do any arithmetic with them?

Craig
Last edited by dunbarx on Wed Mar 16, 2022 3:01 pm, edited 1 time in total.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: subtracting two variables

Post by Klaus » Wed Mar 16, 2022 9:47 am

Samuele wrote:
Wed Mar 16, 2022 12:15 am
...
ahh, it did throw an error that i didn't understand, but i have changed the script to

Code: Select all

put (Var1 - (Var2)) into Var3
and now it seems to be working.
Don't you think that it might be a good idea to ask here what the error means?

Samuele
Posts: 282
Joined: Mon Oct 11, 2021 7:05 pm
Location: Italy

Re: subtracting two variables

Post by Samuele » Wed Mar 16, 2022 9:58 am

Nuuu what it means?
Samuele.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: subtracting two variables

Post by Klaus » Wed Mar 16, 2022 12:23 pm

Samuele wrote:
Wed Mar 16, 2022 9:58 am
Nuuu what it means?
???

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”