[DGD] Referencing an array of variable depth
Robert Forshaw
iouswuoibev at hotmail.com
Sun Feb 29 21:19:48 CET 2004
Imagine a situation where you have an array of arrays, and the first element
of each array may contain another array of arrays. At some point you want to
change a value of all the arrays.
To briefly go over the problem I have, I've got an global array called
action_queue, which stores 'actions' which are arrays storing particular
data. One of the elements of this 'action' might store another action_queue,
or in other words, another array of actions. These are sub-actions.
Now let's say I'm making a function called extend_current_action, which
modifies another element (let's call it interval) of every action stemming
off from the first action. So in one situation you might have the
action_queue array, the first element of that being the current action, and
the first element of the current action being a sub-array (another action
queue), the first element of that being the current sub-action. Let's say
there's one more sub-action below that sub-action... so the first element of
the sub-action contains another sub-array containing another action queue,
the first element of that being the current sub-sub action!
And now what I want to do, is modify the elements of all those actions, the
current action, the current sub-action, and the current sub-sub-action. In
this situation I've described, the depth of the arrays is 3 (or 6 if you
include the queues), but the depth is variable. if it were fixed,
accomplishing what I want would be much easier.
I can't see any clean way to reference the sub-arrays, even if the
extend_current_action is informed of the depth. There are two possible but
undesirable ways I can see to do it:
1) Build a string containing the expression I want to perform on the arrays.
I.e.
string * expressions;
string expression;
int count;
expressions = allocate(depth);
expressions = action_queue[0];
for(count = 0; count < depth; count++)
{
expressions[count] = expression + "[INTERVAL] = time;";
expression[count] += "[SUB_ACTION_QUEUE][0];
}
------------------
There's a glaring flaw in this solution though, namely that there is no
built in way to turn a string into an expression (as far as I'm aware). The
only way to do it is to write it into a seperate file and run that, but that
won't work when trying to reference global variables of a particular object.
2) Do a long and contrived switch statement checking for all the possible
values of 'depth' and then executing the appropriate expression. i.e.
switch(depth)
{
case 0:
action_queue[0][INTERVAL] = time;
break;
case 1:
action_queue[0][INTERVAL] = time;
action_queue[0][SUB_ACTION_QUEUE][0][INTERVAL] = time;
break;
/* aaaand so on... */
Which is unsightly to say the least. There must be a more efficient solution
I've overlooked, surely?
_________________________________________________________________
Stay in touch with absent friends - get MSN Messenger
http://www.msn.co.uk/messenger
_________________________________________________________________
List config page: http://list.imaginary.com/mailman/listinfo/dgd
More information about the DGD
mailing list