Arrays

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
SirWobbyTheFirst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 246
Joined: Tue Jun 30, 2009 11:15 pm

Arrays

Post by SirWobbyTheFirst » Tue Apr 24, 2012 6:04 pm

Hey guys, this is probably a noobish sort of question but I've never had to deal with it before, so bare with me okay. I'm writing a plugin for LC 5.5 which stores information in a multidimensional array however the data can be loaded and unloaded at runtime and one of the checks I need to perform in some cases is to ensure the array is valid I.E. it isn't empty.

And despite the If tArray = Empty Then ... syntax being the most logical method of checking, it doesn't seem to work, tArray equates to empty either way. So I ask how does one check an array to ensure it isn't empty before working on it. I do have the idea to check to make sure a specific property exists but I thought it would be nice to ask here first just in case there is an obvious method and I'm just writing unneccessary code to accomplish it.

PS. Discovered ArrayEncode, ArrayDecode and the ability to store images as binary inside a custom property, just goes to show, you can still find a gem every so often even if like me you've been programming with Rev since the early days. Also going to be using LiveCode next year for my final year project at Uni. :mrgreen:

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Arrays

Post by mwieder » Tue Apr 24, 2012 6:12 pm

Try checking "the keys of" the array and see if it's empty. Otherwise that should return a cr-separated list of the array element keys.
I have a schizophrenic PC, no seriously, it thinks its a Mac.
<g> Thanks to RocketDock, my xp machine also thinks it's a mac. http://rocketdock.com
Last edited by mwieder on Tue Apr 24, 2012 6:15 pm, edited 1 time in total.

SirWobbyTheFirst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 246
Joined: Tue Jun 30, 2009 11:15 pm

Re: Arrays

Post by SirWobbyTheFirst » Tue Apr 24, 2012 6:15 pm

(FACE PALM)

I knew it would happen. Thanks man.

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Arrays

Post by Klaus » Tue Apr 24, 2012 6:26 pm

Hi guys,

Code: Select all

...
if tArray is an array then
  ## do your array stuff here
end if
...
Hey, its LiveCode, isn't it?! :D


Best

Klaus

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Arrays

Post by mwieder » Tue Apr 24, 2012 6:30 pm

Ah, it's one of those not-so-obvious things.
The reason you can't check to see if the array variable is empty is that it can have a value as well as keys:

Code: Select all

put "w1nst0n" into tArray["firstname"]
put "something" into tArray
Now you have tArray not being empty, but not because of the array contents (which are still valid), but because you have put "something" into the variable itself. And putting empty into tArray doesn't clear out the array elements.

SirWobbyTheFirst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 246
Joined: Tue Jun 30, 2009 11:15 pm

Re: Arrays

Post by SirWobbyTheFirst » Tue Apr 24, 2012 7:16 pm

I did not know about the Is An Array syntax, I'll definitely be keeping that in mind for array checking. Now time to quickly add it to my plugin and I shall be able to carry on my merry way. It also further emphasizes what I said in my OP. Even if you are a veteran, programming pops up a gem every now and again that just makes you think. Nice.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: Arrays

Post by dunbarx » Tue Apr 24, 2012 10:26 pm

Note that if you try to read an array variable by simply loading only the array variable name into any ordinary object that can display data, like a field or using the "answer" command, it will show empty. You have to use the "combine" command to convert the array variable into an ordinary variable first. This is best done with a copy, because once deconstructed, it has to be reconstructed with the "split" command.

One of my favorite things to do is to use the debugger as a tool to see an array contents. The debugger shows the keys and the elements nicely.

Here is an example of a counter using an array:

Code: Select all

on mouseUp
   repeat 100
      put any word of "zz aa bb ss dd" & space after temp
   end repeat
   repeat for each word tWord in temp
      add 1 to tWordCount[tWord] --ADD 1 TO THE ELEMENT OF A KEY
   end repeat
   answer tWordCount  --No info
   answer tWordCount["aa"] --likely info
end mouseup
Craig Newman

Post Reply