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 » Sat Sep 08, 2018 5:40 pm

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

Re: Array average

Post by Klaus » Sat Sep 08, 2018 5:45 pm

What did you script so far?

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

Re: Array average

Post by vascoribeiro » Sat Sep 08, 2018 5:48 pm

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

Re: Array average

Post by Klaus » Sat Sep 08, 2018 5:55 pm

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

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

Re: Array average

Post by Klaus » Sat Sep 08, 2018 5:57 pm

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

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

Re: Array average

Post by Klaus » Sat Sep 08, 2018 6:03 pm

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 » Sat Sep 08, 2018 6:05 pm

with an if, then function?

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

Re: Array average

Post by bogs » Sat Sep 08, 2018 7:23 pm

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

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

Re: Array average

Post by dunbarx » Sun Sep 09, 2018 2:40 pm

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

Re: Array average

Post by bogs » Sun Sep 09, 2018 2:52 pm

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

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

Re: Array average

Post by Klaus » Sun Sep 09, 2018 3:08 pm

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

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

Re: Array average

Post by dunbarx » Mon Sep 10, 2018 2:03 pm

Klaus.

Ah.

Craig

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

Re: Array average

Post by vascoribeiro » Tue Sep 11, 2018 8:44 pm

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

Return to “Getting Started with LiveCode - Complete Beginners”