LOCATE Function
When searching for a value in an array, you can use the common “LOCATE” function.
You need to specify the dimension of the array, considering that you’re looking for a string in a:
- valued array (separator= FM), you will use: LOCATE xxx IN array<1> SETTING POS THEN…
- multivalued array (separator= VM), you will use: LOCATE xxx IN array<1,1> SETTING POS THEN…
- subvalued array (separator= SM), you will use: LOCATE xxx IN array<1,1,1> SETTING POS THEN…
Note:
– if the searched value is found, variable “POS” indicates the position in the array (after the “THEN”)
– if not found, “POS” indicates the next position immediately available in the array (after the “ELSE”), e.g. dimension of the array +1.
Example 1:
For an array looking like this: value1:@FM:value2:@FM:value3:@FM:value4
LOCATE "value2" IN array<1> SETTING POS THEN ... END
will fill variable “POS” with “2”.
LOCATE "toto" IN array<1> SETTING POS THEN ... END
will fill variable “POS” with “5”.
Need to find part of the content of a value in an array?
SUBSTRINGS() is the solution!
Example: You have to find a constant that may be PMUF or PMPR or anything else looking like “PM…” in the array R.NEW(SW.AS.TYPE)
SCHED.TYPES = SUBSTRINGS(R.NEW(SW.AS.TYPE),1,2)
LOCATE 'PM' IN SCHED.TYPES<1,1> SETTING POS THEN
END
An alternative to LOCATE is FINDSTR
Compared to LOCATE function, knowing the dimension of the array is not a pre-requisite with FINDSTR.
Example:
YY.OR.MED.FM = '' YY.OR.MED.VM = '' YY.OR.MED.SM = ''
FINDSTR searched_value IN array SETTING YY.OR.MED.FM, YY.OR.MED.VM, YY.OR.MED.SM ELSE YY.OR.MED.FM = “”
IF YY.OR.MED.FM THEN ... END
You just need to adapt the correct data separator, depending on the structure of the ARRAY.