how to add the values of a number of fields.

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
CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

how to add the values of a number of fields.

Post by CAsba » Mon Dec 05, 2022 11:27 am

Hi,
I have 6 fields where the user enters opening bank balances for 6 banks. I need to add the values of the 6 fields to determine if the total is more than zero, which indicates that at least one field is now not "Balance pending". This info is necessary for the next coding step. I have tried

Code: Select all

put field"pg1" + field"pg2" + field"pg3" + field"pg4" + field"pg5" + field "pg6" into field "sum of balances"
and

Code: Select all

put sum(field"pg1", field"pg2") into field "sum of balances"
without success.
How to do it ? Please ?

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

Re: how to add the values of a number of fields.

Post by Klaus » Mon Dec 05, 2022 11:47 am

Did a quick test and this works fine for me:

Code: Select all

...
put field "pg1" + field "pg2" + field "pg3" + field "pg4" + field "pg5" + field "pg6" into field "sum of balances"
...
Bildschirmfoto 2022-12-05 um 11.48.29.png
Bildschirmfoto 2022-12-05 um 11.48.29.png (18.95 KiB) Viewed 1826 times

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: how to add the values of a number of fields.

Post by CAsba » Mon Dec 05, 2022 11:57 am

I get error message 'operators +: error in left operand.
Could it be because some of the fields conents are not numeric ?

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: how to add the values of a number of fields.

Post by stam » Mon Dec 05, 2022 12:03 pm

yes :)
I was just going to say you should make sure there is no comma in the values you're trying to add (keeping in mind that the default decimal point is a period, not a comma as it is in many countries. Needless to say this only works on pure numerical values - but you probably beat me to it ;)

The dictionary is always helpful - look up 'sum':
sum(<numbersList>)

where: numbersList is
  • a sequence of parameters, each a number or evaluating to a number or
  • a single parameter which is or evaluates to
  • a comma-separated list of numbers or
  • an array containing only numbers

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

Re: how to add the values of a number of fields.

Post by Klaus » Mon Dec 05, 2022 12:06 pm

CAsba wrote:
Mon Dec 05, 2022 11:57 am
I get error message 'operators +: error in left operand.
Could it be because some of the fields conents are not numeric ?
Why didn't you write this important info in your posting? 8)
What stam said, you need to check if the field contains a number.

CAsba
Posts: 364
Joined: Fri Sep 30, 2022 12:11 pm

Re: how to add the values of a number of fields.

Post by CAsba » Mon Dec 05, 2022 12:23 pm

Thanks guys, that's set me straight.

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

Re: how to add the values of a number of fields.

Post by dunbarx » Mon Dec 05, 2022 3:34 pm

CAsba.

Do you know how to check that?

Code: Select all

If fld "pg1" is a number and fld "pg1" is a number and...
Craig

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

Re: how to add the values of a number of fields.

Post by dunbarx » Mon Dec 05, 2022 3:46 pm

Just for the exercise, you should do that better, so that any field that is NOT a number is flagged for you.This is best done in a loop, something like:

Code: Select all

on mouseUp
   repeat with y = 1 to the number of flds
      if fld ("pg" & y) is a number then add fld ("pg" & y) to field "sum of balances"
      else
         answer "Field" &&  ("pg" & y) && "needs looking at"
         exit repeat
      end if
   end repeat
end mouseUp
Craig

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

Re: how to add the values of a number of fields.

Post by dunbarx » Mon Dec 05, 2022 3:51 pm

CAsba.

Of course there are likely other fields on the card that are NOT of the "pg" type. Can you see how to only examine those that are?

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7238
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: how to add the values of a number of fields.

Post by jacque » Mon Dec 05, 2022 7:12 pm

Also it is good technique to have a space between the control type and the literal name:

field"pg1" should be: field "pg1"
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”