ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Lookup with more argument in search string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lookup with more argument in search string

    Hi I need to lookup for more than one argument in search string something like this:

    Code:
    Dcl-S Array Char(1) Dim(10);
    Dcl-S Pos Uns(10);
    
    *inlr = *on;
    Array(1) = 'A';
    Array(2) = 'B';
    Array(3) = 'C';
    Array(4) = 'D';
    
    Pos = %Lookup('AB' : Array:2);
    
    Dsply %Char(Pos);
    Return;
    I want to find the firt position which contain 'A' or 'B' is it possible?

    Many thanks

  • #2
    Hi, the solutions I found is to read all the array, staring from the index I want and check for Array value when I match hwhat I want continue...

    Code:
     
     Dcl-S Array Char(1) Dim(10); Dcl-S Pos Uns(10); Dcl-S Idx Uns(10);  *inlr = *on; Array(1) = 'A'; Array(2) = 'B'; Array(3) = 'C'; Array(4) = 'D';  For Idx = 2 to 4;    If Array(Idx) = 'A' or Array(Idx) = 'B';     Pos = Idx;   EndIf;  EndFor;  Dsply %Char(Pos); Return;
    If you have better solutions please tell me.
    Many thanks

    Comment


    • #3
      If you have a fixed array like that, then the only alternative way I can think of is to use %lookup to find both A and B, and then pick whichever value is lower
      Code:
        aPos = %LOOKUP('A':Array);
        bPos = %LOOKUP('B':Array);
        if aPos = 0 or bPos > 0 and bPos < aPos;
          Pos = bPos;
        else;
          Pos = aPos;
        endif;
      I don't know which method is the more efficient.

      But where is this array data coming from? If you are reading it from a table, or you have converted a string into an array of characters, searching the table/string might be more efficient.

      Comment


      • #4
        Also - why are you starting your search at array index 2? That means it will never find A, only B
        And your second search method, using a For loop. That will find the last index, no the first, because you are not ending the loop when you found the first match. So the second match will then overwrite the first.
        I would think you would want this:

        Code:
        For Idx = 1 to %elem(Array);
          If Array(Idx) = 'A' or Array(Idx) = 'B';
            Pos = Idx;
            Leave;
          EndIf;  
        EndFor;

        Comment

        Working...
        X