Show in Frame No Frame
Up Previous Next Title Page Index Contents Search

6.3.10 Iterating over the stack

MetaEdit+ maintains the navigation history, i.e. the graph, objects, relationships etc. used to reach the current point of the generator. Each do, dowhile or foreach block pushes a new element to the navigation history stack, and the end of the block removes that element. Whilst the general output commands like id;1; allow us to pick elements from higher up the stack, sometimes it is useful to reflect over the whole stack. For this, we can use the do stack command, which iterates over the current element, the element one loop out, the element two loops out etc. For instance, we may want to output a little debugging information:
'/* debug trace: '
dowhile stack { id ', ' }
' */' newline
Sometimes we may want to find the closest element of a certain type on the stack. For instance, if we are using do to recurse along relationships in a graph, and want to output some information from the graph itself. Because of the recursion, we will not know how many levels back up the stack the graph itself is. We can use do stack to walk back up the stack until we meet the graph, filtering out other elements using where, and making sure we stop after the first graph using unique:
do stack 
where type = 'MyGraph'
unique type
{  :Graph Name; }
It is also possible to iterate over a truncated version of the stack, omitting elements from the start or the end. To omit elements from the start, i.e. to look at the stack as it was in an outer loop, add a positive level number suffix, e.g. do stack;1. This is useful when you want to find out whether the current element has already been encountered, e.g. to avoid infinite recursion:
do stack;1
where oid = oid;1
unique 'first'
{  $error = 'recursion!' }
To omit elements from the end, i.e. the outermost loops, add a negative level number suffix, e.g. do stack;-1.

Show in Frame No Frame
Up Previous Next Title Page Index Contents Search