Line offset of an item (and other similar functions).
Alex Tweedly
alex at tweedly.net
Tue Jan 25 08:58:14 EST 2005
Raymond E. Griffith wrote:
>>Alex Tweedly wrote:
>>
>>
>>
>>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.
>
The code fragment above does work - that's my working but
inefficient/large version.
What fails is the one-liner using
get the number of lines in item 1 to itemOffset(tNeedle, tData) in tData
That fails because of the CR-within-item issue.
>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.
>
>
As far as I can see, the filter expressions are not capable of
identifying items within a line, they're just too far short of proper
regex, so 'filter' can't be used. If there is a way to do it, please let
me know.
Regex can do it easily enough, so there is the option of
> put "[,^](" & pID & ")[,$]" into tRegex
> put matchChunk(pData, tRegex, t1, t2) into t
> if t then
> get the number of lines in char 1 to t2 of pData
> else
> get 0
> end if
>
I'm not sure if that is much easier or as readily understandable as the
longer scripted version - but it's probably faster.
or Robert suggested the alternative of adding extra itemDelimiters at
the start and end of each line in the data structure, which would be a
confusing but feasible way to do it.
Thanks everyone - I now have a few ways to do it; I'll stick to the
scripted version for maintainability, and if performance becomes an
issue I'll benchmark the regex method and the additional delimiter
version and see which wins.
Thanks
-- Alex.
-------------- next part --------------
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.7.3 - Release Date: 24/01/2005
More information about the use-livecode
mailing list