Counting items in an array

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
makeshyft
Posts: 222
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Counting items in an array

Post by makeshyft » Wed May 01, 2013 3:14 am

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! ;)
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Counting items in an array

Post by sturgis » Wed May 01, 2013 4:10 am

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. :)

makeshyft
Posts: 222
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: Counting items in an array

Post by makeshyft » Wed May 01, 2013 5:45 am

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
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Counting items in an array

Post by bn » Wed May 01, 2013 8:26 am

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

makeshyft
Posts: 222
Joined: Mon Apr 15, 2013 4:41 am
Contact:

Re: Counting items in an array

Post by makeshyft » Thu May 02, 2013 9:58 pm

Thank you....thats very helpful!
Founder & Developer @ MakeShyft R.D.A - https://www.makeshyft.com
Build Software with AppStarterStack for Livecode - https://www.AppStarterStack.com
Save Time with The Time Saver's Toolbox - https://www.TimeSaversToolbox.com

Post Reply