Page 1 of 1

Counting items in an array

Posted: Wed May 01, 2013 3:14 am
by makeshyft
Hi everyone,

I have a rather complex multi-dimensional array..... and I'd like to be able to count the number of entries for various elements of my array. I cannot find a command for this anywhere, but maybe I'm just not looking properly.

So I need to be able to transverse my array and total the counts of various elements. I'm pretty sure there is probably an easy way to do it.

and 2 ..... find out the last number ["ElementName"][??] so that I know what the next one is to add data to the array / element

Much thanks .....

PS I LOOOOOVEEEE LIVECODE! ;)

Re: Counting items in an array

Posted: Wed May 01, 2013 4:10 am
by sturgis
You can:

put the number of lines of the keys of myArray
and it will do just that. Tell you how many keys there are. (the keys of... returns a cr delimited list of keys in unspecified order)

If you need to get the largest key number there are a couple ways to do that to.

put the keys of myArray into tKeys (only gets first level keys, cr delimited as mentioned above)

tKeys now contains a cr delimited list, you can either sort the list (sort lines of tKeys descending numeric) and then get the first line.

Or you can use the max function, but it expects a comma delimited list so you'd first have to replace cr with comma in tKeys and then
put max(tKeys) + 1 into tNExtNumber

And of course you can get the keys of any level, but you'll need to recurse your way through the tree to get what you want. the keys of myArray[1][24]...

Hope something here is helpful, or hope someone else has more suggestions. :)

Re: Counting items in an array

Posted: Wed May 01, 2013 5:45 am
by makeshyft
Thank you for your reply, I think I understand what I must do now. This is gonna be interesting, writing good code to transverse my array and draw data at will :) :o

Re: Counting items in an array

Posted: Wed May 01, 2013 8:26 am
by bn
usually I do it like Sturgis wrote. Arrays can be a bit confusing until it all falls into place and accessing the keys is often easier than other methods.

I played around with "extents" which gives you the min and max key number of a numbered array (subarray) in one command
And the max() and min() function which gives you the max value and min value of arrays that contain numbers (we are talking about the content here not the keys)

If you feel like it you could play with this code in a button and 1 field for output

Code: Select all

on mouseUp
   repeat with i = 1 to 30 -- make an array of 30 numbered keys
      repeat with m = 1 to 50-i  -- make subarrays with decreasing number of numbered keys (49 down to 20)
         put random (300) into tArray[i][m] -- fill the subarrays with random values
      end repeat
   end repeat
   repeat with n = 1 to 30 -- cycle through first level of the array to get at subarrays
      put "min, max of keys of subarray " & n & ": " & tab &  the extents of tArray[n] & cr after tCollect -- get min, max _KEYS_ of subarrays
      put "min value of subarray " & n & ": " & tab & min (tArray[n]) & cr after tCollect -- get min _VALUE_ of subarrays
      put "max value of subarray " & n & ": " & tab & max (tArray[n]) & cr after tCollect -- get max _VALUE_ of subarrays
   end repeat
   delete last char of tCollect -- a return
   put tCollect into field 1
   -- breakpoint -- uncomment to have a look at the array in the debugger
end mouseUp
If I get lost in multi level arrays then I set a breakpoint and the debugger pops up and you can nicely inspect the array + subarrays in the debugger.

Kind regards
Bernd

Re: Counting items in an array

Posted: Thu May 02, 2013 9:58 pm
by makeshyft
Thank you....thats very helpful!