Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: Tim MeagherHi,
There is no simple BRAINscript operator that allows you to do this type of operation.
However, you can get the entire list of input fields, and check to see for each of the fields if the field name matches a specified pattern.
If the field matches the pattern, than you can then add the field value to a count variable, and at the end output this variable.
A (heavily commented) example of how to do this is below:
#The pattern against which to match the fields.
#If you want to make this node relatively generic, you could provide this as a
#parameter, then use parameter substitution in the Script
fieldPattern = ".*_FEE_.*"
#Get the names of all of the input fields
fields = inputFields(1)
#initialize the sum variable
sumVal = 0
#Loop over all input fields
fieldNum = 0
while fieldNum < fields.len() {
fieldName = fields.getItem(fieldNum)
#Check if this field matches the pattern
#If so, get the field value and add to the sum
if (regexIsMatch(fieldName, fieldPattern)) then {
sumVal = sumVal + field(fieldName)
}
fieldNum = fieldNum + 1
}
#Output the sum & the id field
emit id
emit sumVal as "SumVal"
While this will do what you want, it is a pretty inefficient way to perform the operation.
In the example above, the field names will be checked on each execution iteration (i.e. for each input record).
It would make more sense to check the field names, and determine over which fields you want to sum on the first iteration, and then for all subsequent iterations, you can just sum the fields you know match the pattern.
An optimized example (again heavily commented) is shown below:
#The pattern against which to match the fields.
#If you want to make this node relatively generic, you could provide this as a
#parameter, then use parameter substitution in the Script
fieldPattern = ".*_FEE_.*"
#Only look at the field names and determine which fields you want to sum on the first pass
#This just saves on execution time in subsequent passes
if firstExec then {
#Get the names of all of the input fields
fields = inputFields(1)
#Setup a list to store the names of the fields you want to sum
sumFields = list()
fieldNum = 0
#Loop over all input fields
while fieldNum < fields.len() {
fieldName = fields.getItem(fieldNum)
#Check if this field matches the pattern
#If so, add the field name to the list of fields over which to sum
if (regexIsMatch(fieldName, fieldPattern)) then {
sumFields = sumFields.append(fieldName)
}
fieldNum = fieldNum + 1
}
}
#This part will be executed for each input record...
#Initialize the sum variable
sumVal = 0
fieldNum = 0
#Loop over all of the fields to sum, and add the values
while fieldNum < sumFields.len() {
sumVal = sumVal + field(sumFields.getItem(fieldNum))
fieldNum = fieldNum + 1
}
#Output the sum & the id field
emit id
emit sumVal as "SumVal"
Regards,
Tim.