|
|||||||||
|
|||||||||
Related Syntax | Related Concepts | ||||||||
declaration/definition | save |
Syntax
save shelf-name
You can use save
to create a variable that is local in lifetime but global in visibility. This means you can act on that local variable outside the lexical scope in which it occurs, as long as that scope is still executing. To accomplish this, you must first declare a global variable and then, in the appropriate local scope, save that variable. This creates a local variable with the global variable name attached to it:
global integer row-count process do xml-parse document scan file "myfile.xml" output "%c" done element table save row-count set row-count to 0 output "<table border = %"1%">%c</table>" output "<p>The table above has %d(row-count) rows." element row output "<tr>%c</tr>" increment row-count element cell output "<td>%c</td>"
In the code above, the variable row-count is saved in the "table" element rule. It is then a local variable of the "table" element rule, but it has global visibility. We are therefore able to increment it in the "row" element rule.
The principal advantage of using a saved variable rather than just using a global variable directly comes when we encounter recursive structures. Suppose that in our data we have tables within tables. The code above would work for nested tables without modification. When the table rule was called for the nested table, the variable would be saved again. Since save
creates a new local variable which simply borrows the global name, no data is lost no matter how deep the recursion. As each level of recursion exits, the local variable at the next level, whose value has not changed, regains the use of the global name, and processing continues.
The local variable created by a save is initialized to the current value of the global with the same name. If the global is a multi-item shelf, the local is initialized as a shelf of the same size with the same values.
When save
is applied to a stream shelf, every item on that shelf must be either unattached
or closed and attached to a buffer.
save
is a variable declaration, not an action. It must be placed with variable declarations at the beginning of a local scope. It cannot have a condition applied to it.
You cannot save
a function argument inside a function.
Related Syntax save-clear save groups |
Related Concepts Variables |
---- |