[stringtemplate-interest] Iteration over multi-valued attribute using implied counter $i$ and first() rest()
Michael Albanese (malbanes)
malbanes at cisco.com
Thu Jan 8 14:16:27 PST 2009
I have a need to traverse a multi-valued attribute utilizing the
built-in iteration counter $i$ in addition to special casing for
elements first() then rest().
The following worker template that is passed a vector of strings almost
works for my needs.
make_keys_comparison(keys) ::= <<
$first(keys):{(record[$i$] == $it$)}$ $rest(keys):{ and (record[$i$] ==
$it$)}$
>>
I am using the implied $i$ iteration counter for part of the source
generation as it conveniently could serve as the field offset for my
generated lua source code.
...my desired output is an 'if' condition in lua source code. So in a
good world I want the following output where keys = { agent slot module
}.
if (record[1] == agent) and (record[2] == slot) and (record[3] ==
module) then
return record
end
...where the make_keys_comparison() template produces the conditionals
inside the 'if' and the generated array offsets graduate sequentially
from 1 to 'n'.
What I get however is slightly off, and I believe has to do with the
scoping of the implied $i$ counter. Apparently a new instance of the $i$
counter is scoped for both the first() and rest() attribute operators,
and for my needs this hurts as evidenced by the actual output.
if (record[1] == agent) and (record[1] == slot) and (record[2] ==
module) then
return record
end
Here one can see that the record[] offsets are not accurate. They are
affected apparently by the scoping of $i$ between first() and rest().
I'd like to know if using the $i$ is the wrong tact here (or perhaps
there is a bug and this should work ?).
If not is there a way to count and display that count value along with
each element(s) in the multi-valued 'keys' attribute while also
retaining the special casing needed for first/rest ?
Thanks,
Michael
More information about the stringtemplate-interest
mailing list