Array average

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
vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Array average

Post 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:
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Array average

Post by Klaus »

What did you script so far?
vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Array average

Post 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...
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Array average

Post by Klaus »

Did you really use:
...
repeat with x = 1 to x
...
?
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Array average

Post by Klaus »

Ah, OK, confirmed.
Not sure if this is a bug or desired behavior?
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Array average

Post 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!
vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Array average

Post by vascoribeiro »

with an if, then function?
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Array average

Post by bogs »

If / then or case, which ever is your preference.
Image
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Array average

Post 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
Last edited by dunbarx on Sun Sep 09, 2018 2:54 pm, edited 1 time in total.
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Array average

Post by bogs »

You could be right, I don't work much with arrays myself.
Image
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Array average

Post by Klaus »

Take a look into the dictionary, arrays are also supported with "average()".
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Array average

Post by dunbarx »

Klaus.

Ah.

Craig
vascoribeiro
Posts: 33
Joined: Tue Apr 25, 2017 8:13 pm

Re: Array average

Post 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.
Post Reply