array.lookup()



The lookup() method finds a specific element in a simple array or a data structure array.

Parameters
  1. Argument - value to search for

  2. Start Index - position to start searching from

  3. Element Count - number of elements to search from the starting position

  4. Subfield (optional) - if searching a data structure array, specifies which subfield should the function look up

  5. Search type (optional)  - String value of "LE", "LT", "GE", or "GT" can specify a less than or equal to, a less than, a greater than or equal to, or a greater than search instead of an exact match search

Return Value

Returns 0 if no matching element is found; otherwise, the element index is returned.

Example
Example 1: Lookup against a character array

In this example, the lookup starts from the 3rd element and searches 8 elements.

pjs.define("NumIndex", { type: 'unsigned integer', length: 3, decimals: 0 }); pjs.define("arr", { type: 'char', length: 15, dim: 10 }); arr[1] = 'Cornwall'; arr[2] = 'kingston'; arr[3] = 'London'; arr[4] = 'Paris'; arr[5] = 'Scarborough'; arr[6] = 'York'; arr[7] = 'Columbus'; arr[8] = 'Dayton'; arr[9] = 'centerville'; arr[10] = 'vasad'; NumIndex = arr.lookup('vasad', 3, 8);   return NumIndex; //10
Example 2: Lookup against a data structure array

In this example, we are looking for "Patrick" in the name subfield of each array element.

pjs.define("emps", { type: 'data structure', dim: 20, qualified: true, elements: { "name": { type: 'char', length: 25 }, "id": { type: 'unsigned integer', length: 5, decimals: 0 } }}); pjs.define("numEmps", { type: 'integer' }); pjs.define("NumIndex", { type: 'unsigned integer', length: 3, decimals: 0 }); emps[1].name = 'Mary'; emps[1].id = 138; emps[2].name = 'Patrick'; emps[2].id = 10379; emps[3].name = 'Juan'; emps[3].id = 6254; numEmps = 3; NumIndex = emps.lookup('Patrick', 1, numEmps, 'name');   return NumIndex; //2



RPG Equivalent

%LOOKUP()