Eliot’s Tips and Tricks

Loops and #

The loop count operation is always only invoked once. That is a really good design decision in my opinion since it is efficient.

The loop variable is a temporary local variable which is only valid within the loop. If you try and access it afterwards you will get ‘nil’.

Lua defines # as the count operator. This makes it very straightforward to loop through repeating parts of an data-structure – be it an XML document, HL7 message, results for a query – the interface is always the same.

Putting # in front of the object gives it number of children it has. For example #Msg.NK1 might give the number of next of kin NK1 segments. Once you have your head around that concept you can handle any type of repetition. This is a simple example:

The for statement can also be used to print or process every element of a table. For more information on how to do this, see the Lua for tutorial.

Tagged:

One Comment

  1. Pingback: Will You Share Your Code? - iNTERFACEWARE Inc.

Leave A Comment?