Line offset of an item (and other similar functions).

Raymond E. Griffith rgriffit at ctc.net
Tue Jan 25 08:44:07 EST 2005


> Alex Tweedly wrote:
> 
>> J. Landman Gay wrote:
>> 
>>> 
>>> Like this:
>>> 
>>> get the number of lines in item 1 to itemoffset(tString,tData) of tData
>>> 
>> Thank You !
>> I seem to have a mental block against item expressions that cross line
>> boundaries.  My over-structured brain *knows* that items are small
>> things that fit within lines; this is the 3rd or 4th time I've missed
>> something because of that blinkered view.
>> 
> Nope, spoke too soon. That looks convincing, and worked on my initial
> testing - but doesn't actually work properly in all cases.
> 
> The problem is that the CRs are included within items - so given data like
> a,abc,1
> b,def,2
> c,qwe,3
> etc.,  item 1 is "a", item 2 is "abc", item 3 is "1
> b" (i.e. including the cr), item 4 is "def", etc.
> 
> Clearly, that means itemOffset will never match what I think of as items
> that are first (or last) on each line.
> 
> In the above case, I want to be able to search for the line containing
> item "b" and find the second line.
> Currently I'm using
> 
>> function findItemAsLineNumber pItem, pData
>>   local i, L
>>   set wholeMatches to true
>>   --  set the itemDel to TAB
>>   put 0 into i
>>   repeat for each line L in pData
>>     add 1 to i
>>     if itemOffset(pItem, L) > 0 then return i
>>   end repeat
>>   return 0
>> end findItemAsLineNumber
> 
> but I still think there should be an easier (or at least faster,
> shorter, better) way.
> 
> -- Alex.
> 

Hmmm. Come to think of it, there should also be a "lineoffset" function.

 put lineoffset(pItem,pData) into ptestline

No need for a "repeat for each line" construct, although you'd need to turn
wholeMatches to false.  If you are concerned with making sure that you get
an entire item and not a part of it, then you could do a test on it, as,

 repeat while ptestline > 0
   if pItem is an item of line ptestline of pData then
     return ptestline
   else
     put lineoffset(pItem,pData,ptestline) into ptestline
   end if
 end repeat

Just a thought.

Raymond E. Griffith




More information about the use-livecode mailing list