Page 1 of 1

Array average

Posted: Sat Sep 08, 2018 5:40 pm
by vascoribeiro
Hi there!
I'm trying to get the average of an array...
My array is made from the contents of 3 fields and my problem is that I want the average to consider the number of fields with some imput made.
--for example if I have 100, 99, 101 my average is 100 but if I only have 100, 99 my average will be 66.

Must be simple stupid to solve, sorry for being dumb. :lol:

Re: Array average

Posted: Sat Sep 08, 2018 5:45 pm
by Klaus
What did you script so far?

Re: Array average

Posted: Sat Sep 08, 2018 5:48 pm
by vascoribeiro

Code: Select all

 
 
 repeat with x = 1 to x
    put field "PTx" into tPTarray [x]
   end repeat
   
   put average (tPTarray) into field "MEAN1"
   
   
For example if my PT2 don't have an input it will still be considered in my average. :(

EDIT: just noticed that my script didn't work now. I made this "repeat" script to simplify my

Code: Select all

put field "PT1" into tPTarray [1]
and so on...

I tested and it worked because it had values already, when I changed it I got 0 in the average...

Re: Array average

Posted: Sat Sep 08, 2018 5:55 pm
by Klaus
Did you really use:
...
repeat with x = 1 to x
...
?

Re: Array average

Posted: Sat Sep 08, 2018 5:57 pm
by Klaus
Ah, OK, confirmed.
Not sure if this is a bug or desired behavior?

Re: Array average

Posted: Sat Sep 08, 2018 6:03 pm
by Klaus
It is desired behaviour! 8)
From the dicntionary about "average":
...
The average function can also be written like this:
sum(numbersList)/the number of items in numbersList
...
Means EMPTY items (array keys) are also counted.
So you need to check for empty fields before filling the array!

Re: Array average

Posted: Sat Sep 08, 2018 6:05 pm
by vascoribeiro
with an if, then function?

Re: Array average

Posted: Sat Sep 08, 2018 7:23 pm
by bogs
If / then or case, which ever is your preference.

Re: Array average

Posted: Sun Sep 09, 2018 2:40 pm
by dunbarx
I thought the issue was that the OP wanted to get the average of an array. The average function will only work with clear data.

Unless I am just not getting it, the array must be converted to an ordinary dataSet with the "combine" command, and then the average function can be used. And yes, an empty field must be considered both to exist and to contain "0".

Craig Newman

Re: Array average

Posted: Sun Sep 09, 2018 2:52 pm
by bogs
You could be right, I don't work much with arrays myself.

Re: Array average

Posted: Sun Sep 09, 2018 3:08 pm
by Klaus
Take a look into the dictionary, arrays are also supported with "average()".

Re: Array average

Posted: Mon Sep 10, 2018 2:03 pm
by dunbarx
Klaus.

Ah.

Craig

Re: Array average

Posted: Tue Sep 11, 2018 8:44 pm
by vascoribeiro
Just a follow up...

I solved my issue like this:

Code: Select all

   repeat with x=1 to the number of fields in group "grp0"
      if field x of group "grp0" is not "" then
         put field x of group "grp0" into tArrayPT[x]
      end if
   end repeat
With this I can do means and stdev of the array correctly :)

Thank you for your help guys.