note
samtregar
I think the most complex data-structure I've ever created was for [cpan://HTML::Template]. The module has two main structures - an array of ops including variables, text, loops and conditionals (@stack) and a hash of parameter names to the corresponding variable or loop (%map). The trick is that both the map and the stack point to the same underlying storage. This allows me to do something like this to set the value for a variable:<p>
<code>${$map{var_name}} = "text for var_name";</code>
<p>
Then when I get to the place where I want to use the variable on the stack I just do:<p>
<code>$output .= ${$stack[$i]};</code>
<p>
Since the same scalar is referenced from both @stack and %map I can get access to them both quickly and with no copying required.
<p>
Now, setting up this structure is indeed a royal pain in the ass but the payoff in output speed makes it all worthwhile. (Of course, these days all the real speed demons have moved on to [cpan://HTML::Template::JIT]!)
<p>
-sam
175409
175409