Reputation: 387
I have created a data structures array and i want to look based on key combination. i.e. in the below DS i want to lookup and get index if tblName and tblElement and tblDivision are matching and then return tblRes value.
D TblAryDs DS qualified dim(9999)
D tblName 3
D tblElement 10
D tblDivision 5
D tblRes 2
//Not Sure How to do the below lookup
idx = %lookup(tblName:TblAryDs(*).tblName) && %lookup(tblElement:TblAryDs(*).tblElement) && %lookup(tblDivision:TblAryDs(*).tblDivision);
if idx > *zeros;
return TblAryDs(*).tblRes;
endif;
Upvotes: 1
Views: 6417
Reputation: 23783
D TblAryDs DS qualified dim(9999)
D key
D tblName 3a overlay(key)
D tblElement 10a overlay(key:*next)
D tblDivision 5a overlay(key:*next)
D tblRes 2a
/FREE
idx = %lookup(tblName + tblElement + tblDivision
:TblAryDs(*).key);
if idx > *ZEROS;
return TblAryDs(idx).tblRes;
endif;
/END-FREE
Note: as coded above, %LOOKUP()
will search all 9999 elements. If you have to search repeatedly for different values, add the ASCEND
keyword to the array and use SORTA
to sort it prior to searching. Lastly, keep track of how many elements are actually used in the array.
idx = %lookup(tblName + tblElement + tblDivision
: TblAryDs(*).key : nbrElemUsed);
This way %LOOKUP()%
will perform a much faster binary search of only the active elements.
Upvotes: 5