EACH

Parameters

OrderNameTypeDesc
1listanyThe list to iterate through
2funcfunctionThe function to execute for every item in the list

Returns

The stacks returned from calling func on each item in the list , followed by a single const with the number of items added.

Description

This command runs the func function for every item in the list array.

This command is mainly used if you want to return data that is broken up on a per-item basis rather than as a whole.

This command is very similar to FOR, except this function iterates through a list rather than through the stack.

When the func function is run, the index element (the item you're iterating into) is set as the 'index' variable, and is placed as the first element of the stack.

Note that the behaviour of GET and SET is slightly different when in the each loop. Calling set will only set a value for the nested function - you can't set values that will be accessible to the calling function.

However, GET can access information stored by any function in the calling hierarchy.

Example

To demonstrate the function, let's look at a simple example.

('Alpha', 'Beta', 'Gamma') ASSET RELS will return a single list of relationships that are present on any of the three assets. This might be useful in some scenarios, but we'd like to know which relationships are present in the individual assets.

We could do this by doing the same basic function three times…

'Alpha' ASSET RELS 'Beta' ASSET RELS 'Gamma' ASSET RELS would do the job, as it would return three distinct lists, one for each asset. But we need to know each of the asset names before-hand, and it would be painful for large lists.

('Alpha', 'Beta', 'Gamma') ASSET [ RELS ] EACH will run the function in the square brackets (RELS) for every item in the list returned by ASSET.

To walk through the process…

When 'Each' is called, the stack looks like this…

Main StackSub-Stack
[Asset List] Alpha - Beta - Gamma
[Function] RELS

Now, 'Each' consumes the asset list and the function itself, resulting in an empty stack.

Main StackSub-Stack

The func function has an independent stack, that initially contains the item from the list as the first item in the stack.

Main StackSub-Stack
[Empty][Asset List] Alpha
[Command] RELS

The RELS command is executed, resulting in a list of relationships being added.

Main StackSub-Stack
[Relationship List] Location, Profile, Water, 240VAC

Now that the function is complete, the list is copied onto the main stack.

Main Stack
[Relationship List] Location, Profile, Water, 240VAC

This continues for all three items in the asset list, after which a constant is added that gives the total number of items processed (similar to the COUNT function).

Main Stack
[Relationship List] Location, Profile, Water, 240VAC
[Relationship List] Location, Profile, 240VAC
[Relationship List] Location, Profile, Control, Hydraulic Pressure, 240VAC
[Const] 3

To make your results clearer, you can use the NAME function to apply names to your returned stack items.

('Alpha', 'Beta', 'Gamma') ASSET [ RELS 'index' GET NAME ] EACH

See Also