Page 1 of 1

Index in a multi-dimensional Array/Matrix

Posted: Tue Jan 29, 2008 5:31 am
by talk2shaanc
I would like to find index of a string in a MATRIX/Multi-dimensional array.
Does anyone know a function to get the same?

Posted: Tue Jan 29, 2008 5:55 am
by WoMaWil
there are several look into manual or help

Posted: Tue Jan 29, 2008 6:36 am
by talk2shaanc
WoMaWil wrote:there are several look into manual or help
if you are refering to Index function then that wont help.

I will explain my requirement. assume i have 3X3 matrix as

Code: Select all

Bill     Jon      Pam
Male   Male    Female
25      29       62
I have requirement where i need to search "Pam" in the matrix and then read all other elements related to "Pam". So in the end I want the result to be "Pam", "Female", "62"...

In my example I have mentioned the dimension (3X3); but in actual dimension is also unknown/variable.

The index function will return 7, assuming no spaces added for "Bill"/"Jon

Posted: Tue Jan 29, 2008 7:30 am
by kcbland
If your example was in 3x3 dynamic array:

Code: Select all

example=""
example<1,1>="BILL"
example<1,2>="JON"
example<1,3>="PAM"
example<2,1>="MALE"
example<2,2>="MALE"
example<2,3>="FEMALE"
example<3,1>="25"
example<3,1>="29"
example<3,1>="62"
You would use the LOCATE statement to search one row of the matrix for values. Upon finding the matching value, the statement returns the position in the array. That position can be used to reference the remaining rows. The statement has an if-then-else structure to let you handle it being found or not:

Code: Select all

LOCATE "JON" IN EXAMPLE<1> SETTING PSN THEN
   GENDER = example<2,PSN>
   AGE = example<3,PSN>
END ELSE
   GENDER = "UNKNOWN"
   AGE = 0
END

Posted: Tue Jan 29, 2008 9:08 pm
by ray.wurlod
FIND may be easier to manage than LOCATE.

Or you could make a 1x9 matrix, copy from one to the other (using MAT keyword) and search that using LOCATE. If name found, add six to the where-found value to identify the element number of the desired age.

Or you could MATBUILD a nine-element dynamic array.

There are even more possibilities, you should be well under way with just these and those of the other posters.