Reading an Array... "backwards"

Posted by OctavioOlguin on 06-Sep-2016 18:15

Greetings...

Everybody of us, know that we can

DEFINE VARIABLE myARRAY AS CHAR NO-UNDO EXTENT 5 INITIAL ["A","B","C","D","E"].
DISPLAY "Third position stores " myArray[3].

To get a message that displays sort of "Third position stores  C".

Question is.

Is there an instruction that could allow me to get:

"Letter D in in position 4"

without resorting to a DO i =1 to length_of_array:  if myArray[i] = value_searched THEN Display.....

I'm talking about some LOOKUP kind of function or instruction that allows to search the values inside an array?

-- Javascript would do .indexOf("D")

Thanks.

All Replies

Posted by scott_auge on 06-Sep-2016 20:52

I think you are underestimating the number of computation .indexOf() has.

But an object could be created:

A = new WhatChaMaCallit(myArray).

D = A:indexOf("D")

L = A:lengthOf()

etc.

Using a temp-table in it instead of an array, allows more flexibility, the constructor simply walks the array and make a record for each element.

Then ya could:

A:Add("F")

A:Remove("D")

etc.

Posted by Patrick Tingen on 07-Sep-2016 02:05

If you want to know this because you have to do this a gazillion times in a loop, then I think a TT with all the values from your array will be your best friend. I am not aware of some sort of reversed-index instruction to get the position of an element in an array.

Other alternative may be to place the array elements in a comma separated list and use the LOOKUP function. But either way, you need to do some extra things before you find the element's position.

Posted by Peter Judge on 07-Sep-2016 08:17

You should also add this an Idea (enhancement request) too.

This thread is closed