Iterate Through Elements in a DataSet

For the individual element style of indexing, it is necessary to manually iterate over each element. A method to iterate over an unknown number of axes is presented, it is a powerful tool and simpler to maintain than nested loops.

Expressions can become difficult to work with, a simple pf.DataSet.ForAllValues method is provided iterating through a DataSet. This method is particularly useful when a script should iterate over an unknown number of axes.

The ForAllValues method accepts the following parameters:

Value function
This is the name of the function that will be executed while looping over all the axes. The function used in ForAllValues must adhere to a predefined format discussed below.
Target DataSet
The target DataSet is used to determine the axes that will be iterated over. All of the indices in the target DataSet will be reached. It is also typically the result that will be modified.
Additional parameters
Any number of additional parameters can be included and will be passed on to the value function. The additional parameters can be Lua values, tables or other DataSet results.

The value function used in the ForAllValues call must be defined with the following parameters:

Index
This parameter is always required and its value will be determined by the ForAllValues function. The index allows the DataSet to be accessed as if it were reshaped into a 1D vector. This is as opposed to the single element indexing described previously, where the DataSet is indexed like a multidimensional array. The value function is called by the ForAllValues function for each axes entry and with every call the index parameter is updated.
Target
The target DataSet that is supplied to the ForAllValues function is passed on to the value function in this parameter. This is the DataSet that determines the axes that will be looped over.
Additional parameters
Any additional parameters that were added to ForAllValues function call will be passed on to the value function.
The following example illustrates the use of the ForAllValues function and its accompanying value function definition.
nf1 = pf.NearField.GetDataSet("Horn.StandardConfiguration1.NearField1")
nf2 = pf.NearField.GetDataSet("modNF_indiv")
nfAverage = pf.NearField.GetDataSet("modNF_indiv")

function average(index, target, source1, source2) 
  target[index].EFieldComp1 = 0.5*(source1[index].EFieldComp1 + source2[index].EFieldComp1)
  target[index].EFieldComp2 = 0.5*(source1[index].EFieldComp2 + source2[index].EFieldComp2)
  target[index].EFieldComp3 = 0.5*(source1[index].EFieldComp3 + source2[index].EFieldComp3)
  target[index].HFieldComp1 = 0.5*(source1[index].HFieldComp1 + source2[index].HFieldComp1)
  target[index].HFieldComp2 = 0.5*(source1[index].HFieldComp2 + source2[index].HFieldComp2)
  target[index].HFieldComp3 = 0.5*(source1[index].HFieldComp3 + source2[index].HFieldComp3)
end
pf.DataSet.ForAllValues(average, nfAverage, nf1, nf2)
return nfAverage
Note: That in this example, it was never necessary to define a loop. Instead, a function is written that explains what should happen to a given element. Any number of DataSet sources can be provided as source inputs. The result of the calculation is then stored in the target DataSet.