Sum array field
Posted in 2009
Topics: Stored Procedures & SPL
What is the best way to handle summing money values in an 'input array'? Since the user may change a previously entered value I would need to make sure to back out the amount in a before field and then add it back into the total in a after field. define mv_arr array[200] of record type char(2), amount decimal(16,2) end record before field amount let lv_ac=arr_curr() let lv_total = lv_total - mv_arr[lv_ac].amount after field let lv_ac=arr_curr() let lv_total = lv_total + mv_arr[lv_ac].amount display lv_total to formonly.total Or would it be better to use a function like display_totals() and just include a call to that in the 'after field'? Or maybe I should just stop worrying about the niggle :-)? function display_totals() define lv_a smallint define lv_total decimal(16,2) let lv_total = 0 for lv_a = 1 to 200 if mv_arr[lv_a].amount is not null then let lv_total = lv_total + mv_arr[lv_a].amount end if end for display lv_total to formonly.total end function
loc wrote: > What is the best way to handle summing money values in an 'input > array'? Since the user may change a previously entered value I would > need to make sure to back out the amount in a before field and then > add it back into the total in a after field. > > > define mv_arr array[200] of record > type char(2), > amount decimal(16,2) > end record > > before field amount > let lv_ac=arr_curr() > let lv_total = lv_total - mv_arr[lv_ac].amount > > after field > let lv_ac=arr_curr() > let lv_total = lv_total + mv_arr[lv_ac].amount > display lv_total to formonly.total > > > Or would it be better to use a function like display_totals() and just > include a call to that in the 'after field'? Or maybe I should just > stop worrying about the niggle :-)? > > > function display_totals() > define lv_a smallint > define lv_total decimal(16,2) > > let lv_total = 0 > > for lv_a = 1 to 200 > if mv_arr[lv_a].amount is not null then > let lv_total = lv_total + mv_arr[lv_a].amount > end if > end for > > display lv_total to formonly.total > end function Just add them all and don't worry.