Shop OBEX P1 Docs P2 Docs Learn Events
LOOKUPZ(Index: v1, v2..v3, etc) : Value what are ranges? — Parallax Forums

LOOKUPZ(Index: v1, v2..v3, etc) : Value what are ranges?

LOOKUPZ(Index: v1, v2..v3, etc) : Value
Lookup value (values and ranges allowed) using 1-based index, return value (0 if index out of range)
What are ranges?
Regards and Thanks
Bob (WRD)

Comments

  • An example of a range is 12..25 meaning all of the values between 12 and 25 inclusive.

  • x := 150
    a := 100
    b := 300
    w := lookup(x: A..B) '
    debug(udec(x),udec(y),udec(w) 'w= 249
    x := 50
    a := 100
    b := 300
    w := lookup(x: A..B)
    debug(udec(x),udec(y),udec(w)) 'w= 149
    x := 2
    a := 100
    b := 300
    w := lookup(x: A..B)
    debug(udec(x),udec(y),udec(w)) 'w=101
    If lookupz used instead of lookup the result for above is 250,150,102 not sure how this would ever be used?

  • The lookup functions tend to be used to convert a contiguous range to a non-contiguous range -- the lookupz variant changes the offset of the first element of the list. This example makes that easy to see:

      repeat x from 1 to 5
        y := lookup(x : "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        term.tx(y)
        term.tx(" ")
    
      term.tx(13)
    
      repeat x from 1 to 5
        y := lookupz(x : "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        term.tx(y)
        term.tx(" ")
    
      term.tx(13)
    

    The output:

    With lookup the search value of zero can be thought of as "don't convert" when used like this:

      if (x)
        x := lookup(x : a..b)
    

    In this case if x is 0 it won't be converted. That said, it can be simplified like this

      x := lookupz(x : x, a..b)
    

    In either case, if x is zero it won't be modified, otherwise it will get set to the xth position within the range of a..b.

  • thanks i wasn't getting the index to position and i see you can also use arrays within the lookup

    repeat x from 1 to 4
           repeat y from 1 to 9
              w := lookup(x: V1[y],V2[y],V3[y],V4[y])
              debug(udec(x,y,w))
           repeat
    DAT
    V1 long 0,101,102,103,104,105,106,107,108,109
    V2 long 0,201,202,203,204,205,206,207,208,209
    V3 long 0,301,302,303,304,305,306,307,308,309
    V4 long 0,401,402,403,404,405,406,407,408,409
    
  • that is a new one for me

    cool,

    Mike

Sign In or Register to comment.