Getting index values with @Formula

I’m doing quite a lot of @Formula coding these days and I’m finding the “new” index capability of @Formula very helpfull. I know it was introduced in release 6 but I’m just starting to use it – old habits die hard.

In that connection I frequently have two arrays of data where I need to find the index of a key value (“Key”) in the first array (“keys”) and get then get the entry at the same index in the other array (“data”). I have found no ArrayGetIndex-like function in @Formula so I’m resorting to a @For-loop to find the correct index and then getting the value from the other array.

keys := @GetProfileField("MyProfile"; "Keys");
data := @GetProfileField("MyProfile"; "Data");
result := "";
@For(n:=1; n<=@Elements(keys); n:=n+1;
   @If(keys[n] = Key;
      @Set("result"; profile_kortlink[n]);
      ""
   )
);
result

Anyone who knows of an easier way?

Published by

lekkim

Positive, competent, out-spoken, frank and customer focused architect and developer with a strong foundation in web, cloud and product development. I'm a strong advocate for API first and cloud based solutions and development. I have a knack for being able to communicate and present technically complicated matters in conference, customer and training settings. I've previously acted as team member and leader in a product organisation.

4 thoughts on “Getting index values with @Formula”

  1. No there is no other easier way that I know of. In 5 I implemented this using @SubSet, but the other developers would complain of the complexity.

    The only difference in my implementations is combining the two lists into one, because the two list might get out of sync. One list might have 5 elements and the other has 4 elements thus causing errors. The only issue is having a unique string (I choose %~%) to separate the values.

    The only other thing you could as an exit for the @For loop.

    Example:

    REM {KeyData is stored in the following format Key%~%Data};
    keyData := @GetProfileField("MyProfile"; "KeysData");
    result := "";
    @For(n:=1; n<=@Elements(keyData); n:=n+1;
       @If(keyData[n] = @Word( keyData[n] , "%~%" , 1 );
          @Set("result"; @Word( keyData[n] , "%~%" , 2 ) ) : n := @Elements( keyData);
          ""
       )
    );
    result
    
  2. Well, duh…

    keys := @GetProfileField(“MyProfile”; “Keys”);
    data := @GetProfileField(“MyProfile”; “Data”);
    n:=@Member(keys; key);
    result:=@If(n=0; “not found”; data[n]);
    @If(@IsError(result); “no data”; result)

    Found in designer help, “Working with lists”

    Or hav I misunderstood something?

Comments are closed.