7.88.2 LOC_ENTRY Examples
Example 1: Locate the first entry in a list called #ORDERLINE where the product of quantity and price is greater than 1000:
DEF_LIST NAME(#ORDERLINE) FIELDS(#ORDLIN #PRODUCT #QUANTITY #PRICE) TYPE(*WORKING)
LOC_ENTRY IN_LIST(#ORDERLINE) WHERE('(#QUANTITY * #PRICE) *GT 1000')
Example 2: A "table file" called #COUNTRY (a list of countries) contains fields #CCODE (country code), #CMNEM (country mnemonic) and #CNAME (country full name). Use a working list to create a subroutine that will minimize I/O's to the file (ie: replace FETCH commands with LOC_ENTRY commands):
SUBROUTINE NAME(GET_CNTRY) PARMS((#GETCODE *RECEIVED)(#CMNEM *RETURNED) (#CNAME *RETURNED))
DEFINE FIELD(#GETCODE) REFFLD(#CCODE)
DEF_LIST NAME(#COUNTRIES) FIELDS(#CCODE #CMNEM #CNAME) TYPE(*WORKING) ENTRYS(100)
LOC_ENTRY IN_LIST(#COUNTRIES) WHERE('#CCODE = #GETCODE')
IF_STATUS IS_NOT(*OKAY)
FETCH FIELDS(#COUNTRIES) FROM_FILE(COUNTRY) WITH_KEY(#GETCODE)
ADD_ENTRY TO_LIST(#COUNTRIES)
ENDIF
ENDROUTINE
Note that this routine makes no allowances for a country code not being found in the file #COUNTRY. The entry is added to the list regardless of whether the country was found or not.
Note also the use of field #GETCODE (instead of #CCODE) in the parameter list. If this was not done, the WHERE condition in the LOC_ENTRY command would have to be expressed as WHERE('#CCODE = #CCODE'), which is always true, so the first entry would always be retrieved.
When a field referenced in a WHERE condition is part of the working list, it is the occurrence of the field in the working list that is evaluated, not the actual field as it is known in the program.
Thus the condition WHERE('#CCODE = #GETCODE') is actually saying "where the value of field country code in a working list entry is equal to the value of field get code in the program".