Arrays: new and old keys, i

David Bovill david at architex.tv
Thu Sep 11 16:27:36 EDT 2008


2008/9/11 Trevor DeVore <lists at mangomultimedia.com>

> On Sep 11, 2008, at 3:10 PM, David Bovill wrote:
>
>  1) Ability to reference an multi-dimensional keys dynamically. Right now
>>> we
>>> have to build the array key reference and then use 'do'.
>>>
>>
>> Yes - that was the first problem I came across. I wanted to write a
>> recursive function, and since you don't know how deep you will need to go
>> you have to use "do" which destroys the reason to use arrays 9ie speed) in
>> the first place. This makes it useless to use as a native data structure
>> for
>> things like arrays - or at least impossible to create generic libraries
>> for
>> tree / xml data structures.
>>
>
> Generic conversion of XML to Array and Array to XML is possible without
> dynamic keys once you have decided on a way to represent the XML as an
> array. The XML conversion routines in the stack I provide with the article
> can convert between XML and arrays and don't need dynamically generated
> keys. I'm using the XML to Array conversion routines extensively right now
> with web services and have been quite happy with them.
>
> I've attached latest version of these handlers at the bottom of this email
> for reference. The entry points are ConvertArrayToXML() and
> ConvertXMLToArray(). Look at ConvertXMLNodeToArray() for an example of the
> recursive call.


Oh great! Just found your stack - just what I wanted. Your functions are
almost identical to my outline_ToXML functions! And now we can do them with
arrays - fab!

I have another scenario where I had to resort to 'do' though. I'm converting
> SQL queries to a hierarchal array but unlike XML SQL results have no sense
> of hierarchy. So I have a couple of 'do' statements in the code which I will
> promptly replace when/if the engine is updated to support dynamic keys.


I can't quite follow the details of this but I am sure it is a very powerful
and important feature to add. I love the ability to have variable properties
for instance. It is so powerful to be able to do this without speed
penalties:

put "name" into someProperty
put the someProperty of btn 1


Yes, it is unfortunate that we have to resort to custom sorts (see
> SortArrayKeysWithXMLOrdering in XML code) or numeric sorts in order to
> iterate through keys sequentially. Hopefully that will be addressed in the
> near future.


I really like the way lua does this - in particular their concept of
meta(data)tables. You can define a default array in this metatable, and then
if a vlaue in your array is not set it returns the default value. This is
great for things like translations - and I've often use this data structure
going back to Hypercard days. Maybe its not core - but I ffigure if you are
going to implement a new datastructure why not learn from the best. Adding
things like sorted indexes could be done to the metatable without affecting
any backward compatability issues. Really no more than a suggestion /
thought :)

Great functions though Trevor - thanks. Looks like it will be worth using
arrays for tree structures. Do you know by the way what the limits on keys
are, and can we now pass arrays to seprop handlers? That would be very
usefull for me as I often define getprop handlers that return arrays - but
have to limit this as there is no way to implement a corresponding setprop.



More information about the use-livecode mailing list