Subtracting a field value from another field value

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

Subtracting a field value from another field value

Post by CAsba » Tue Apr 25, 2023 11:13 am

Hi,
I've tried

Code: Select all

put fld "xxxx" into txxxx
   put fld "fstock" into tfstock
   subtract txxxx from tfstock
   #put (fld "fstock" - fld "xxxx") into fld "fstock"
   
   put tfstock into tstock
   put tstock into theDataA["stock"]
Such a simple thing (!!) it would seem, but it's cost me many frustrating minutes, without success.
Please help.

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

Re: Subtracting a field value from another field value

Post by Klaus » Tue Apr 25, 2023 11:57 am

Did you "extract" theDataA from your datagrid first?
Don't see this in the code snippet.

And then you need of course write the array theDataA back into your datagid.

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

Re: Subtracting a field value from another field value

Post by CAsba » Tue Apr 25, 2023 12:12 pm

I think I did all that, here's the complete snippet:

Code: Select all

put fld "xxxx" & TAB into tCode
   put the dgtext of grp "datagrid 3" into tText
   put lineoffset(tCode,tText) into tline
   if tLine = 0 then
      beep
      answer "No such code!"
      exit to top
   end if
   
   put line tLine of tText into tFoundLine
   #put tfoundline into fld "xxxx"
   set itemdel to TAB
   set the dghilite of grp "datagrid 3" to tfoundline 
   
   put the dgHilitedLines of group "Datagrid 3" into theLine   
   put the dgDataOfLine[theLine] of group "Datagrid 3" into theDataA
   put theDataA["stock"]into fld "fstock"
   put fld "xxxx" into txxxx
   put fld "fstock" into tfstock
   subtract txxxx from tfstock
   #put (fld "fstock" - fld "xxxx") into fld "fstock"
   
   put tfstock into tstock
   put tstock into theDataA["stock"]

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

Re: Subtracting a field value from another field value

Post by Klaus » Tue Apr 25, 2023 12:35 pm

Yor forgot this at the end:

Code: Select all

...
   put tfstock into tstock
   put tstock into theDataA["stock"]

   ## This one:
   set the dgDataOfLine[theLine] of group "Datagrid 3" to theDataA
...

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

Re: Subtracting a field value from another field value

Post by Klaus » Tue Apr 25, 2023 12:38 pm

Hint:

Code: Select all

...
 set the dghilite of grp "datagrid 3" to tfoundline 
...
the dgHilite is BOOLEAN value, so either TRUE or False!
Take a look in the dictionary for further infos.

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

Re: Subtracting a field value from another field value

Post by CAsba » Tue Apr 25, 2023 1:05 pm

Hi Klaus,
Thanks for that.
There's still a problem though at the line

Code: Select all

   subtract txxxx from tfstock
error type subtract: error in source expression.
I'm baffled.

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

Re: Subtracting a field value from another field value

Post by Klaus » Tue Apr 25, 2023 1:15 pm

Hm, syntax is correct!?
Make sure there are definitively NUMBERS in those fields.

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

Re: Subtracting a field value from another field value

Post by dunbarx » Tue Apr 25, 2023 1:17 pm

CAsba.

Not sure what your actual data is, but that error comes when the values in those variables are not numeric. You cannot, say, subtract "42" from "A106"

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”