# Arrays: new and old keys, i

Trevor DeVore lists at mangomultimedia.com
Thu Sep 11 18:20:23 CDT 2008

```On Sep 11, 2008, at 5:55 PM, Björnke von Gierke wrote:

> Now I understand, I didn't consider not to tell rev how many sub
> arrays there would be. I have no clue when this would be useful
> though, maybe you can explain that?

Sure. One thing that the ability to dynamically specify a key would do
is open up additional algorithms. Let's take the XML for example.

The XML conversion routines I posted earlier use recursion in order to
create a multi-dimensional array from an XML document of unknown
parent/child relationships. Every time the function finds children of
a node the function calls itself recursively in order to process those
children.

If we could reference array dimensions dynamically then we could
perform the conversion using an iterative algorithm instead. As we
moved through each dimension in the XML tree we would keep track of
the key specifying the location of that dimension in the array we are
creating. Some example values:

theDimensionKeys[1] = ["root"]
theDimensionKeys[2] = ["root"]["lessons[1]"]
theDimensionKeys[3] = ["root"]["lessons[1]"]["steps"]["step[3]"]

As we processed child nodes we would add the child node to the array
by using the multi-dimensional key for the parent node (child node
dimension - 1). No recursion necessary. Perhaps you could get slightly
better performance using the iterative vs. recursive approach.

> At first I wanted to write an example function that uses the
> paramCount, so it would automatically fill the array in a switch (at
> some point you do know what the max subarray count would be). But
> that'd be way too complex, and you'd need one for putting, and one
> for getting, so they'd suddenly be a kind of strange cprops. Using
> do is certainly the simplest way, but if you really care for speed,
> maybe such a function could be useful, despite the complexity?

There are probably some scenarios where a switch statement would
suffice. For now I get by with do (i.e. the slowness of 'do' isn't too
bad). I'm confident that we won't always be subject to using known
keys with multi-dimensional arrays though so I leave 'do' in in hopes
that I can change the code to something fast and elegant someday :-)

> This is a rather interesting topic to me :D

I think so too. New data structures are so much fun.

Regards,

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