Average function

Jim Ault JimAultWins at yahoo.com
Sun Jan 28 14:07:58 EST 2007


On 1/28/07 10:19 AM, "Scott Rossi" <scott at tactilemedia.com> wrote:

> Recently, Tom Vawter wrote:
> 
>> Is there an average function that will not consider blank fields?  For
>> instance, if I have 10 and 8 in two fields and an empty third field, I
>> want to take the average of the numbers in the three fields and get 9
>> rather than 6.
> 
> Could you do this?
> 
>  function nonemtpyAverage
>     put 0 into N
>     put 0 into tTotal
>     repeat with F = 1 to number of flds
>        add fld F to tTotal
>        if fld F <> "" then add 1 to N
>     end repeat
>     return tTotal/N
>  end nonemtpyAverage
> 
> You may need to limit the number of fields you want to check.

Or you could do one of these:

get fld distance1 & cr & fld distance2 & cr & fld distance3
filter it without empty
replace cr with comma in it
get sum(it)/the number of items in it
answer it

for numerous fields -------------------
get empty
repeat for each word FN in "apples oranges bananas pears"
   get it & fld FN & cr
end repeat
filter it without empty
replace cr with comma in it
get sum(it)/ the number of items in it
answer it

to get all "distance" fields [1,2,5,10]----------------
repeat with x = 1 to the number of controls
  if ("field "&quote& "distance") is in the name of control x  then
  put control x & cr after newList
end if
end repeat
filter newList without empty
replace cr with comma in newList
get sum( newList )/ the number of items in newList
answer newList 

Hope this helps

Jim Ault
Las Vegas

On 1/28/07 10:19 AM, "Scott Rossi" <scott at tactilemedia.com> wrote:

> Recently, Tom Vawter wrote:
> 
>> Is there an average function that will not consider blank fields?  For
>> instance, if I have 10 and 8 in two fields and an empty third field, I
>> want to take the average of the numbers in the three fields and get 9
>> rather than 6.
> 
> Could you do this?
> 
>  function nonemtpyAverage
>     put 0 into N
>     put 0 into tTotal
>     repeat with F = 1 to number of flds
>        add fld F to tTotal
>        if fld F <> "" then add 1 to N
>     end repeat
>     return tTotal/N
>  end nonemtpyAverage
> 
> You may need to limit the number of fields you want to check.





More information about the use-livecode mailing list