Search code examples
livecode

How to find the line offset containing a specific number


I am trying to use the following code

put 7 into lFoodID

lineoffset (lFoodID,gArrFood) into tArrFoodLine

to find the line that contain the number 7 in the array below

17  Banana
20  Beans
2   Beef
1   Bread
8   Cabagge
6   Chicken
5   Eggs
15  Ice Cream
3   Mango
7   Pork
18  Rice
4   Salad
19  fried fish

It's returning 1. I know that this is because 17 contains the number 7. I have tried

set the wholeMatches to true

but that does not work either. I believe that regex (^(7) should work but I can figure out how to use regex in lineoffset.


Solution

  • I'm not sure what you're really after and I wonder if your data really look like what you have provided here. I assume that your data are as displayed, but this may lead do a solution that's slightly different from what you really want.

    If you want to get the product associated with an index, you can use the following script

    put fld 1 into myList
    replace space&space with tab in myList
    repeat until (tab&tab is not in myList and space&space is not in myList)
       replace space&space with space in myList
       replace tab&tab with tab in myList
    end repeat
    split myList by cr and tab
    put myList[7] into myProduct
    put myProduct
    

    MyProduct contains the product name. Note that you won't need the repeat loop if your data is formatted properly. If you really want to have the index, use this:

    put fld 1 into myList
    put 7 into myIndex
    if word 1 of myList is not myIndex then
       put number of lines of char 1 to offset(cr & "7" & space ,myList) of myList into myLine
    else
       put 1 into myLine
    end if
    put myLine
    

    MyLine contains the complete record in your list.