Documentation & Books

Richard Gaskin ambassador at fourthworld.com
Wed Jul 7 15:36:29 CDT 2004


Judy Perry wrote:

 > Here's my real problem:  I barely got a "D" in the linear
 > algebra class that dealt with matrices (which I am assuming
 > is similar if not identical to arrays).

You beat me:  I got an F.  No kidding.

Fortunately arrays are much simpler.

 > So, I am clueless.  I am one step ahead of a newbie who has
 > never heard the term before (and, hence, wouldn't know to
 > search for it, much less know what to do with any results)
 > in that I think I have a vague idea of what it is and what
 > it can be used to do (for example, keep track of character
 > stats in a game, including previous locations in which
 > something interesting was revealed or for the purposes of
 > map revelations/display and the like) but that's about it.
 > I read the docs and still cannot proceed to step 1 (or, is
 > that, step 0?).  I suspect it could also be used to
 > generate displays for a game such that one wouldn't need
 > a separate card for each location.

But you could probably do the same with a delimited list as well. 
Sometimes it's not bad to solve a problem with what you know, and branch 
out in stages only as needed.

 > So, what I do is use a bunch of global variables to keep
 > track of these things (and, one card for every display).
 > I'm perhaps aware that this is not the best way to do such
 > things, but it is the way I CAN do such things.

Getting results is rarely a bad place to start. :)

It's hard for me to come up with a good example using your situation 
without knowing more about it, but I'll give you one that helped me 
understand some of the unique value of arrays.

I had a circumstance where I needed to keep track of stuff globally but 
couldn't know in advance how many of those things I would need so I 
couldn't declare separate globals for each.

I could have used a delimited list, but it would have looked something like:

   steve,100
   bob,980
   jane,444

...and it would have been cumbersome to write:

   set the wholeMatches to true
   get item 2 of lineOffset(tPerson,gMyGlobal)

...just to get that one value.

Enter arrays:

An array is just a collection of values in which each value has a name. 
  That name can be a number or an alphanumeric string.  This label is 
commonly called a "key", and if it helps I sometimes remind myself that 
it's the key that unlocks the value.

In the example above, we could store a value with something like:

   put 100 into tMyArray["steve"]

...and get it with:

   get tMyArray["steve"]

Conveniently, you don't need to keep track of what's in the list -- you 
can get a list of keys like this:

   get the keys of tMyArray

...which returns a return-delimited list of the labels of all of the 
array elements.

Even more convenient, you can store items in an array in one move by 
using the split command.  If we had the delimited list above we could 
tuck it into an array in one line:

   put tMyList into tMyArray
   combine tMyArray with return and comma

The "with" part specifies the delimiters to be used for breaking the 
text chunks into array elements.

Since arrays are dependent on specific memory structures, they cannot be 
easily stored directly.  You can't, for example, say "write tMyArray to 
file whatever".  But you can use the combine command to put an array 
into a chunk, effectively reversing the effect of the split command:

   put tMyArray into tMyList
   combine tMyList with return and comma

To delete an array element use the "delete variable" command:

   delete variable tMyArray["steve"]

You can also use variables to access array elements.  This example will 
store a list of control names in an array by number:

  repeat with i = 1 to the number of controls
    put the short name of control i into tMyArray[i]
  end repeat


Play with those and post more questions as they come up.  I'm sure 
others have as many questions about arrays as I did when Kevin and Scott 
Raney patiently helped me through them on the MetaCard list way back when.

Sure beats linear algebra, eh? :)

-- 
  Richard Gaskin
  Fourth World Media Corporation
  ___________________________________________________
  Rev tools and more:  http://www.fourthworld.com/rev



More information about the use-livecode mailing list