Add. & Sub. Error - Operators -: error in left operand

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
Davidng
Posts: 4
Joined: Tue Sep 15, 2015 8:46 am

Add. & Sub. Error - Operators -: error in left operand

Post by Davidng » Sat Sep 19, 2015 2:03 pm

Hi, All

I am having some difficulty getting a button to work that adds the result of two text entry field sections, and then subtracts the addition of the two sections from an original text entry field. I am including the code below and explain the throughout process along the way. Still new at this so forgive me if the answer is straight forward.

on mouseUp
local A1, A2
put the text of field "IncomeEntryField" into A1 -- this is the text entry field where an amount is entered
put field "LongTerm1Amt" + field "LongTerm2Amt" into field "LongSubTotal" -- This adds two text entry fields into a label
put field "ShortTerm1Amt" + field "ShortTerm2Amt" into field "ShortSubTotal" -- This adds two text entry fields into a label
put the text of field "LongSubTotal" + the text of field "ShortSubTotal" into field "StageField" -- This adds the label values together
put the text of field "StageField" into A2
put A1 - A2 into field "IncomeAfterDebtLabel"
end mouseup

It applies correctly however when I run the application I get the error:

Button "LongShortCalculate" error Operators -: error in left operand

It is rather strange if I substitute "IncomeEntryField" with "LongSubTotal" for example it works, but the moment I use the other field it fails.

Hope you can help, appreciate the assistance.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Add. & Sub. Error - Operators -: error in left operand

Post by dunbarx » Sat Sep 19, 2015 2:49 pm

Hi.

It "applies" (compiles) correctly because the code looks good to the parser.

But you are getting a runtime error, because something in your data is not a number. LC will not know what to do with:

Code: Select all

add 25 to 50Q
So try this. Put the following handler in the card script:

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 goodNumber
Now run your stuff, but apply as follows:

Code: Select all

put goodNumber(field "LongTerm1Amt") + goodNumber(field "LongTerm2Amt") ...
This will validate user input, in case they add something untoward, including invisible characters that you may not find by inspection.

Craig Newman

Davidng
Posts: 4
Joined: Tue Sep 15, 2015 8:46 am

Re: Add. & Sub. Error - Operators -: error in left operand

Post by Davidng » Sat Sep 19, 2015 3:11 pm

Craig, I take my hat off to you. This worked perfectly! Plus more importantly very important lesson learned. Thank you! :D :D :D

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: Add. & Sub. Error - Operators -: error in left operand

Post by golive » Sun Sep 20, 2015 2:26 am

How does one ensure only numeric input when user enters data into a field?

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Add. & Sub. Error - Operators -: error in left operand

Post by Simon » Sun Sep 20, 2015 5:06 am

errr... what Craig posted?
Or are you after at time of input?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Add. & Sub. Error - Operators -: error in left operand

Post by dunbarx » Sun Sep 20, 2015 3:15 pm

Oh ho, you want to validate at the front, not at the rear. Actually better.

Your field script ought to check what the user is typing, eh? And only pass characters that you intend, eh?

There is a message, "keyDown", and control structures "if" and "pass". Can you write a one-line handler that will allow only numeric input into a field?

One line, I said. Get going...

Craig

EDIT: There is one tiny issue with that one liner. Resolving that issue in that same line is possible, but woud be excessive. Anyway, write back when you find it.

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: Add. & Sub. Error - Operators -: error in left operand

Post by golive » Mon Sep 21, 2015 7:51 am

dunbarx wrote:Oh ho, you want to validate at the front, not at the rear. Actually better.

Your field script ought to check what the user is typing, eh? And only pass characters that you intend, eh?

There is a message, "keyDown", and control structures "if" and "pass". Can you write a one-line handler that will allow only numeric input into a field?

One line, I said. Get going...

Craig

EDIT: There is one tiny issue with that one liner. Resolving that issue in that same line is possible, but woud be excessive. Anyway, write back when you find it.
Thanks for the keyDown and pass suggestion.

Code: Select all

on keyDown tCharacter
   if tCharacter is in "0123456789."then pass keyDown
end keyDown
:idea:

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Add. & Sub. Error - Operators -: error in left operand

Post by dunbarx » Mon Sep 21, 2015 1:52 pm

Right on.

But what about that little issue I mentioned? Do you know what it is? Do you know how to, er, handle it?

Hint: You can certainly have several "5's" in your data, but can you have several....

Craig

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: Add. & Sub. Error - Operators -: error in left operand

Post by golive » Tue Sep 22, 2015 3:15 am

dunbarx wrote:Right on.

But what about that little issue I mentioned? Do you know what it is? Do you know how to, er, handle it?

Hint: You can certainly have several "5's" in your data, but can you have several....

Craig
OIC, more than one dot :D


This works:

Code: Select all

on keyDown tCharacter
   switch 
      case "." is in tCharacter and "." is in fld "fldNumberOnly" 
         break
      case tCharacter is in "0123456789."
         pass keyDown
         break
   end switch
end keyDown
One line, no can do. Is that possible?

Post Reply