Removing a local property tip

We all know how useful local variables are since they provide a way to avoid "contamination" of the pipeline with spurious temporary/working variables in PilotScript. But, as the help says: "If an expression makes a change to a local property when processing a data record, that change remains until the next data record is processed within that component." But more precisely, if an expression changes the local variable, then the change remains until the local variable is assigned again. For example, the following protocol:



 when the Customer Manipulator contains:

if Index = 2 then
    #flag := true;
end if;
if #flag is defined then
    flag := #flag;
    flag_is_defined := true;
end if;

The output is:

Index

flag

flag_is_defined

1



2

true

true

3

true

true

As you can see #flag is assigned on the 2nd record, but it remains assigned on the 3rd. But what if we want the local variable to be destroyed on each invocation of the Custom Manipulator? I haven't found a "supported" way of removing a local variable, Remove(''#flag') doesn't do anything.

A workaround is to use: ChangePropertyType(#flag, 'Scitegic.Value.NullValue');

So this code:

ChangePropertyType(#flag, 'Scitegic.Value.NullValue');
if Index = 2 then
    #flag := true;
end if;
if #flag is defined then
    flag := #flag;
    flag_is_defined := true;
end if;

Will result in this output:

Index

flag

flag_is_defined

1



2

true

true

3



Now #flag is no more defined for the 3rd record. But I think it would be better to have Remove('#flag') work or may be a more explicit function, like RemoveLocalProperty(property), for example.


Answers 3
JF
JF 2018-07-14

Local variables exist within the scope of the component, not within each individual record.  So, in your example, when you set #flag_is_defined on record 2 to true, it remains true in record 3.  That is normal behavior.

One thing you might be able to do, if you indeed want the local variables to be removed after each individual record, would be to put that component into a Run-to-completion subprotocol.  I.e. right-click the component, and choose collapse to subprotocol.  Then set  the Runtime option Run-To-Completion to true.  This means that the subprotocol treats each individual record as a standalone operation.  Sometimes this may be just fine, but there may be other times where this is not just fine.  For your specific example, this will work.  See the screen shots enclosed.

comment imagecomment image
OS
OS 2018-07-15

Yes, but run-to-completion has other side effects. I can't think of any reason why a simple Remove could not work on local properties.

KS
KS 2018-07-16

A quicker, and more standard programming practice would be to initialize the variable at the beginning of your Expression.  Putting #flag := false at the beginning of your script starts the script with a known value every time a record is processed.  Rather than rely on if a local variable 'is defined' or not, use it's 'true' or 'false' value as the basis of your IF statement.  This is definitely more efficient than Run To Completion!