QlikView Expressor: Using the Iterator Function

    Originally published on 07-19-2011 10:30 PM

     

    In the operators in which you add scripting to implement your processing logic (the operators in the Transformers grouping and the Read Custom and Write Custom operators), the last statement executed is typically

    return output

    This statement causes the operator to emit an initialized record to  the next operator.  Generally this statement will suffice, but there are situations with the Read Custom and Write Custom operators (and in very special situations with the transform or aggregate operators) where you will want to emit multiple records.  In these situations, follow the return keyword with the name of a function, the iterator function, that does the work of initializing and emitting the multiple records.

    Let's see how this works.

    An  iterator function is capable of returning at least two values: an initialized record or nil.  If the function returns an initialized record, the Expressor runtime emits the record then invokes the iterator function again.  If the function returns nil, the Expressor runtime does not emit a record and does not invoke the iterator function; the function terminates and processing within the operator ends.  When used with the Read Custom or Write Custom operators, an iterator function may  also return false, which causes the expressor runtime to invoke the iterator function again, but does not emit a record.

    The easiest  way to create an iterator function is to write a function that performs  your desired logic and then enter a call to this function as part of the  return statement.  For example, when used within the transform  operator, you could develop code similar to the following.


    function myfunction()
       if some_condition then
         -- initialize output record
         return output
       else
         return nil
       end
    end

    function transform(input)
       -- process the contents of input
       return myfunction
    end


    It is equally acceptable to define an anonymous iterator function within the transform function.


    function transform(input)
       -- process the contents of input

       return function()
         if some_condition then
           -- initialize output record
           return output
         else
           return nil
         end -- end if block
       end -- ends anonymous function
    end -- ends transform function

     

    Note that when used within the Read Custom or Write Custom operators, return true (rather than nil) to indicate that the iterator function should terminate and false to indicate that the iterator function will not emit a record but should be invoked again.  As an alternative to returning true and false, an iterator function in the Read or Write Custom operators can return expressor.ScriptSupport.FlowComplete or expressor.ScriptSupport.SkipRecord.