## 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: Klaus, FourthWorld, heatherlaine, kevinmiller

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

### Array average

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.

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

### Re: Array average

What did you script so far?

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

### Re: Array average

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: 10627
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

### Re: Array average

Did you really use:
...
repeat with x = 1 to x
...
?

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

### Re: Array average

Ah, OK, confirmed.
Not sure if this is a bug or desired behavior?

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

### Re: Array average

It is desired behaviour!
...
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

with an if, then function?

bogs
Posts: 2764
Joined: Sat Feb 25, 2017 10:45 pm

### Re: Array average

If / then or case, which ever is your preference.

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

### Re: Array average

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: 2764
Joined: Sat Feb 25, 2017 10:45 pm

### Re: Array average

You could be right, I don't work much with arrays myself.

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

### Re: Array average

Take a look into the dictionary, arrays are also supported with "average()".

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

### Re: Array average

Klaus.

Ah.

Craig

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

### Re: Array average

``````   repeat with x=1 to the number of fields in group "grp0"