Line offset of an item (and other similar functions).
Raymond E. Griffith
rgriffit at ctc.net
Tue Jan 25 08:28:32 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. Since L is a separate chunk each time (a single Line), I don't know
why it won't work. However, try this.
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 pItem is among the items of L then return i
end repeat
return 0
end findItemAsLineNumber
You might also try
If pitem is an item of L then return i.
There is also a "filter" pData with pitem -- but you'd need to put pData
into a temporary variable since it eliminates the rest of the data list
except for the line it finds. It also has the the ability to use simple
regex arguments in the filter.
Raymond E. Griffith
More information about the use-livecode
mailing list