ABC of arrays

Dave Cragg dave.cragg at lacscentre.co.uk
Wed Apr 1 18:58:11 EDT 2015


> On 1 Apr 2015, at 21:48, Graham Samuel <livfoss at mac.com> wrote:
> So, following on another mail I just sent, I think people can get confused by sub-arrays and what ‘the keys’ actually looks at. More examples needed in the docs, perhaps.
> 
> Hope this is not seen as wilful misunderstanding - it’s not meant to be!

I can understand that "sub-arrays" can be confusing. And perhaps the use of multiple sets of square brackets makes it more so. Where possible, I try to avoid using multiple square brackets. So instead of this:

----------------------
  put "1" into tA[1]
  put "2.1" into tA[2][1]
  put "2.2" into tA[2]["surprise me, Jean"]
----------------------

I might use this:
-----------------------
 put "1" into tA[1]

 put "2.1" into tB[1]
 put "2.2" into tB["surprise me, Jean"]

 put tB into tA[2]
------------------------
I think it makes it clearer that the "sub-array" is actually a quite separate array. It just happens to be kept in one of the elements of the other array.

As Michael pointed out, the ConvertXMLToArray example was probably not the easiest of examples to deal with. XML doesn't easily convert to an array, and the function uses complex key names to try to preserve the xml structure.

I don't know if it helps, but below is an example of the kind of thing I've used to try to illustrate arrays to people before.

---------------------------
on mouseUp
   put empty into field 1
   
   put "John" into tPerson["firstName"]
   put "Smith" into tPerson["lastName"]
   put "M" into tPerson["gender"]
   put "1994-04-04" into tPerson["dateOfBirth"]
   
   put tPerson into tPeople[1]
   
   put "Sue" into tPerson["firstName"]
   put "Jones" into tPerson["lastName"]
   put "F" into tPerson["gender"]
   put "1992-03-03" into tPerson["dateOfBirth"]
   
   put tPerson into tPeople[2]
   
   //lets look at the keys
   put the keys of tPeople & cr & cr after field 1
   put the keys of tPeople[1] & cr & cr after field 1
   put the keys of tPeople[2] & cr & cr after field 1
   
     //lets look at the data
   put the keys of tPeople into tTopKeys
   repeat for each line tKey in tTopKeys
      put tPeople[tKey] into tPerson
      put the keys of tPerson into tPersonKeys
      repeat for each line tPersonKey in tPersonKeys
         put tPersonKey & ": " & tPerson[tPersonKey] & cr after field 1
      end repeat
      put cr after field 1
   end repeat
end mouseUp
---------------------------

Cheers
Dave Cragg





More information about the Use-livecode mailing list