Arrays: new and old keys, i

Trevor DeVore lists at mangomultimedia.com
Fri Sep 12 13:22:26 EDT 2008


On Sep 12, 2008, at 12:37 PM, Mark Wieder wrote:

> But I'm sorry to say that I can't think of a situation in which
> chronological order would be helpful. Obviously you've got a scenario
> where this works - could you enlighten me?

I can try :-) Let's look at a few examples.

I will start with a simple example that mostly affects syntax. You  
provided an example of using integers in order to access your array  
values in a logical order. That is a simple enough repeat loop as we  
can use the extents property:

======
repeat with i = 1 to item 2 of the extents of theLessonsA
	...
end repeat
======

If Rev remembered the order you added the keys (assuming you added  
sequentially starting at 1) your code would look like this:

======
repeat for each key theIndex in theLessonsA
	...
end repeat
======

I prefer the later code myself.



As a second example let's look at keys that aren't numeric but whose  
order is important. If you are converting XML to an array you may just  
want to use the XPATH syntax for storing arrays of children nodes.  
Something like this:

[1]["step[1]"]
[1]["step[2]"]

If I want to iterate through the keys of ["lessons"] in the correct  
order I have to extract the keys, sort and then iterate:

======
put the keys of theLessonsA[1] into theKeys
set the itemdelimiter to "["
sort theKeys numeric by the last item of each -- 1], 2], 3], etc.
repeat for each line theKey in theKeys
     ...
end repeat
======

If Rev remembered the order that the keys were added then the code  
would be:

======
repeat for each key theIndex in theLessonsA
	...
end repeat
======

Much cleaner.



To finish with, let's look at another XML example I grabbed from one  
of the w3schools samples.

<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>

If I convert the XML to an array it might look like this:

["breakfast_menu"]["food"]["1"]["name"]
["breakfast_menu"]["food"]["1"]["price"]
["breakfast_menu"]["food"]["1"]["description"]
["breakfast_menu"]["food"]["1"]["calories"]

["breakfast_menu"]["food"]["2"]["name"]
["breakfast_menu"]["food"]["2"]["price"]
["breakfast_menu"]["food"]["2"]["description"]
["breakfast_menu"]["food"]["2"]["calories"]

What if I want to display the array in a field in Revolution?

======
repeat with i = 1 to item 2 of the extents of  
theArray["breakfast_menu"]["food"]
     put "Food item:" && i & cr after theText
     repeat for each key theKey in theArray["breakfast_menu"]["food"][i]
         put theKey & ":" && theArray["breakfast_menu"]["food"][i] 
[theKey] & cr after theText
     end repeat
end repeat

set the text of field 1 to theText
======

But wait, there is a problem here. Since the order of keys of  
Revolution is undefined I am not guaranteed that the list of food  
properties will print in the same order for each food item. That means  
I have to enforce the order myself:

Predefined:
repeat for each item theKey in "name,price,description,colories"

Sort:
put the keys of theArray["breakfast_menu"]["food"][i] into theKeys
sort theKeys

There would be no need to go through these extra steps if the array  
always spit out keys in the order they were added.

Regards,

-- 
Trevor DeVore
Blue Mango Learning Systems
ScreenSteps: http://www.screensteps.com
Developer Resources: http://revolution.bluemangolearning.com



More information about the use-livecode mailing list