Question Find An Element In An Array

davidvilla

Member
Hi,
Is there any function like LOOKUP or INDEX or ENTRY to find an element by value within an array? I know we can loop through the array elements and compare it with the desired value. But I was looking for an inbuilt function to achieve this.
 

Stefan

Well-Known Member
Not in pure ABL as far as I know, but if you can use the .Net bridge the following seems to work (.Net arrays are 0 indexed, so + 1):

Code:
define variable cc as char no-undo extent 3 initial [ "one", "two", "three" ].  

message
   System.Array:IndexOf( cc, "one" ) + 1 skip
   System.Array:IndexOf( cc, "two" ) + 1 skip  
   System.Array:IndexOf( cc, "three" )  + 1
view-as alert-box.

A quick comparison with a pure ABL IndexOf function:

Code:
function indexOf returns integer (
   i_carray as character extent,
   i_c as character
):
   def var ii as int no-undo.
   
   do ii = 1 to extent( i_carray ):
      if i_carray [ii] = i_c then
         return ii.
   end.
 
   return 0.

end function.

Does point out that the .Net bridge is a bit expensive in this case - 160 ms for 1000 .Net repeats vs 30ms for 1000 ABL repeats. Increasing the size of the extent to 200 (without any extra data) moves it to 300ms vs 200ms. But if I look for a value that is not in the list the tables are turned.

YMMV
 
Last edited:

davidvilla

Member
Thanks Stefan

I used a character list (comma separated) and used the LOOKUP function.. achieved what I wanted
but, if the list is too big, i will have to use an array and traverse the array elements to get the required element.. all right!
 

RealHeavyDude

Well-Known Member
Since OE10.0A+ you can also use a longchar for separated lists. But, as Stefan already said: YMMV.

Heavy Regards, RealHeavyDude.
 
Top