[SOLVED] Math - ugh - How to add up fields of strings

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
karmacomposer
Posts: 256
Joined: Wed Apr 27, 2011 2:12 pm

[SOLVED] Math - ugh - How to add up fields of strings

Post by karmacomposer » Fri Jul 20, 2018 5:45 am

I have four text fields for the user to enter dollar amounts - hopefully no $ (do we use the delimiter to parse out a possible "$" - must be only numbers

Here is what I wrote (I KNOW it's wrong):

Code: Select all

 --Premium Subtotal Calculation
      
      put the value of field "fldBiWeekPrem1" into varPrem1Add
      put the value of field "fldBiWeekPrem2" into varPrem2Add
      put the value of field "fldBiWeekPrem3" into varPrem3Add
      put the value of field "fldBiWeekPrem4" into varPrem4Add
      put the value of varPrem1Add+varPrem2Add+varPrem3Add+varPrem4Add into varSubtotal
      set the text of field "fldPremSubTotal" to it
How do I add up fields into a subtotal field?

Mike
Last edited by karmacomposer on Fri Jul 20, 2018 4:53 pm, edited 1 time in total.

ClipArtGuy
Posts: 205
Joined: Wed Aug 19, 2015 4:29 pm

Re: Math - ugh - How to add up fields of strings

Post by ClipArtGuy » Fri Jul 20, 2018 5:54 am

Code: Select all

repeat with x=1 to 4
      add fld ("fldBiWeekPrem"&x) to tCount
      put tCount into field "fldPremSubTotal"
end repeat

karmacomposer
Posts: 256
Joined: Wed Apr 27, 2011 2:12 pm

Re: Math - ugh - How to add up fields of strings

Post by karmacomposer » Fri Jul 20, 2018 6:02 am

That threw an error in the line:

Code: Select all

add fld ("fldBiWeekPrem"&x) to tCount
The field names are the same, so fldBiWeekPrem1 and so on is correct.

Did I do something wrong?

Mike

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

Re: Math - ugh - How to add up fields of strings

Post by Klaus » Fri Jul 20, 2018 7:56 am

Hi Mike,

That should definitively work!
What is the error exactly?

Best

Klaus

P.S.
I recommend to take a look at these stacks to get the basics of Livecode, maybe start with "Controls": http://www.hyperactivesw.com/revscriptc ... ences.html

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

Re: Math - ugh - How to add up fields of strings

Post by Klaus » Fri Jul 20, 2018 8:59 am

Hi Mike,

another hint which saves a lot of typing:
"the value of fld xyz" is not neccessary, only in seldom cases, just do:

Code: Select all

...
put fld "my field" into tFieldContent
...
## And
## put the value of varPrem1Add+varPrem2Add+varPrem3Add+varPrem4Add into varSubtotal
put varPrem1Add + varPrem2Add + varPrem3Add + varPrem4Add into varSubtotal
...
And SPACES do not hurt, but make the scripts more readable!


Best

Klaus

karmacomposer
Posts: 256
Joined: Wed Apr 27, 2011 2:12 pm

Re: Math - ugh - How to add up fields of strings

Post by karmacomposer » Fri Jul 20, 2018 12:58 pm

The error was

Code: Select all

add: error in source expression
I'll try the other way.

Mike

karmacomposer
Posts: 256
Joined: Wed Apr 27, 2011 2:12 pm

Re: Math - ugh - How to add up fields of strings

Post by karmacomposer » Fri Jul 20, 2018 1:02 pm

The second one:

Code: Select all

put varPrem1Add + varPrem2Add + varPrem3Add + varPrem4Add into varSubtotal
      set the text of field "fldPremSubTotal" to it
Gives me the error:

Code: Select all

(Operators +: error in left operand
Any other suggestions?

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

Re: Math - ugh - How to add up fields of strings

Post by Klaus » Fri Jul 20, 2018 1:05 pm

Hm, sound like there could be non-numeric content in one of the fields.
Did you double-check their values (in the debugger)?

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

Re: Math - ugh - How to add up fields of strings

Post by dunbarx » Fri Jul 20, 2018 1:10 pm

Clearly someone left a "$" in one of the fields.

LC cannot add '3" to "$7"

I have had this in my library since 1990:

Code: Select all

function goodNumber tText
   repeat for each char theChar in tText
      if theChar is in ".0123456789" then put theChar after temp
   end repeat
   return temp
end goodNumberr
So you would want to:

Code: Select all

  put goodnumber(field "fldBiWeekPrem1") into varPrem1Add
But it would be even better to validate the input text when the user is working. Do you know how to do this?

Craig Newman

karmacomposer
Posts: 256
Joined: Wed Apr 27, 2011 2:12 pm

Re: Math - ugh - How to add up fields of strings

Post by karmacomposer » Fri Jul 20, 2018 1:39 pm

I forgot to mention that all four fields have the following code to ensure digits only:

Code: Select all

# Allow only digits - no letters or symbols

on keyDown pKey
   if pKey is not a number then
      ## If the parameter is not 0-9
      ## prevent text entry
   else
      ## Allow text entry by passing the keyDown message
      pass keyDown
   end if
end keyDown
Could this change things?

Mike

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

Re: Math - ugh - How to add up fields of strings

Post by Klaus » Fri Jul 20, 2018 2:01 pm

Hi Mike,

this is VERY strange!? :shock:
I can offer to take look at your (stripped down) stack, if you like, just send it to my email address.

And here another typing-saving advice:

Code: Select all

on keyDown pKey
   if pKey is a number then
      ## Allow text entry by passing the keyDown message
      pass keyDown
   end if
end keyDown
:D


Best

Klaus

karmacomposer
Posts: 256
Joined: Wed Apr 27, 2011 2:12 pm

Re: Math - ugh - How to add up fields of strings

Post by karmacomposer » Fri Jul 20, 2018 4:53 pm

All figured out. I found my error - and it was a biggie.

Thanks for the help guys.

Mike

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

Re: [SOLVED] Math - ugh - How to add up fields of strings

Post by dunbarx » Sat Jul 21, 2018 10:50 pm

Glad to hear you worked it out.

But just to close this properly, limiting text entry only to digits rarely works, unless only integers are desired.

What about the poor decimal point?

Craig

karmacomposer
Posts: 256
Joined: Wed Apr 27, 2011 2:12 pm

Re: [SOLVED] Math - ugh - How to add up fields of strings

Post by karmacomposer » Mon Jul 23, 2018 2:33 pm

I accounted for the decimal point as well.

Mike

Post Reply

Return to “Talking LiveCode”