Arrays: new and old keys, i

Trevor DeVore lists at mangomultimedia.com
Sat Sep 13 09:10:42 EDT 2008


On Sep 13, 2008, at 12:36 AM, Mark Wieder wrote:

> Friday, September 12, 2008, 10:22:26 AM, you wrote:
>
>> <breakfast_menu>
>>        <food>
>>                <name>Belgian Waffles</name>
>>                <price>$5.95</price>
>>                <description>two of our famous Belgian Waffles with  
>> plenty of real
>> maple syrup</description>
>>                <calories>650</calories>
>>        </food>
>>        ...
>> </breakfast_menu>
>
> Sorry, I still don't get it. XML is hierarchical, not chronological.

In the second example I provided chronology may be implied though. If  
you fetched data from a web server that performed a custom sort on the  
data then the order of the children nodes would be the order of the  
sort. The convenience that PHP provides by remembering the order the  
keys were added is beneficial in this case as you aren't forcing the  
developer to use an integer based array every time. They can key the  
array however suits them.

> The XML attributes "name", "price", "description", and "calories" are
> in no particular order in the xml document, chronological or
> otherwise. If you want to enforce an order on them after the fact
> you have to do it yourself in some fashion.

Why force the developer to enforce an order after the fact? That is  
the point of having arrays remember the order the keys were added. It  
can simplify code in these circumstances.

In addition once you have an array that maintains ordered keys it  
opens up some additional syntax options in the engine. Some pseudo  
code follows.

*Add a value as the last element in a dimension*

put "title 1" into theDataA[]
put "title 2" into theDataA[]


*Delete element*

put "title 1" into theDataA[]
put "title 2" into theDataA[]

delete element 1 of theDataA

## There is no whole where the first element used to be.
## The first element now has the value "title 2"
put element 1 of theDataA
    "title 2"


*Add elements in middle of array*

put "title 1" into theDataA[]
put "title 3" into theDataA[]

put "title 2" after element 1 of theDataA[]

Internal order would now be
"title 1"
"title 2"
"title 3"


*Reordering*

put "title 1" into theDataA[]
put "title 2" into theDataA[]
put "title 3" into theDataA[]

move element 3 after element 1 of theDataA

Internal order would now be
"title 1"
"title 3"
"title 2"


*Array sorting*

## Create an array keyed by a unique id
put 15 into theID
put "Zebra" into theDataA[theID]["animal"]
put 12 into theID
put "Bunny" into theDataA[theID]["animal"]

## Sort the array by animal name.
sort theDataA text by theDataA[]["animal"] ascending

## Internal order of unique ids has changed based on sort
put element 1 of theDataA
     "12" - Bunny id
put element 2 of theDataA
     "15" - Zebra id


Multi-dimensional arrays are a powerful addition to Revolution as it  
provides a basis for more powerful array manipulation features in the  
engine moving forward. I feel that ordered keys form the basis for a  
lot of those features.

Regards,

-- 
Trevor DeVore
Blue Mango Learning Systems
www.bluemangolearning.com    -    www.screensteps.com



More information about the use-livecode mailing list