a couple hurdles I had with Rev
Jim Ault
JimAultWins at yahoo.com
Thu Jun 15 21:24:08 EDT 2006
Perhaps a slight improvement for the count of items/lines appears at the
end.
On 6/15/06 5:18 PM, "Alex Tweedly" <alex at tweedly.net> wrote:
> Mark Smith wrote:
>
>>
>> On 16 Jun 2006, at 00:47, Jared Smith wrote:
>>
>>> Here are a few things that I've been confused about:
>>>
>>> 1) How do I find the number of times a certain character appears in a
>>> string? The "offset" function only finds the first occurance, it
>>> doesn't count them.
>>>
>>
>> This function should work:
>>
>> function charCount tText,tChar
>> set the itemDelimiter to tChar
>> return the number of items in tText - 1
>> end charCount
>>
> Watch out for the case where the last character in the string is the one
> you are interested in; because there isn't an item after it, this gives
> the wrong result - I'd use
>
> function charCount tText,tChar
> set the itemDelimiter to tChar
> if char -1 of tText = tChar then
> return the number of items in tText
> else
> return the number of items in tText - 1
> end if
> end charCount
>
Another approach I use for the "last item empty" scenario you mention, Alex,
is
return the number of items in (tText&null)
--now the count will be correct
---and very unlikely that you would choose an itemDel of null
The same for repeat loops...
repeat with x = 1 to the number of items in (tText&null)
--do the right number of loops
end repeat
repeat with x = 1 to the number of lines in (tText&null)
Jim Ault
Las Vegas
More information about the use-livecode
mailing list