Counting Chars by ASCI Values

Jim Ault JimAultWins at
Wed Mar 1 15:12:56 EST 2006

On 3/1/06 9:34 AM, "Mark Smith" <mark at> wrote:

> Jim's solution seems the best, but if the data is big, then the
> 'number of items in (textStr & null)' seems to slow things down - I
> guess that textStr is being copied with the null char appended, and
> if it's big, that's a lot of work. I think it's therefore more
> efficient to test for char -1 of textStr being the char tested for,
> and if it is, then add 1 to the count. Also, shouldn't the count be
> 'the number of items in textStr -1?
> so I ended up with this:
> on yetAnotherWay
>    put fld 1 into tString
>    repeat with n = 1 to 30
>      if (n >= 10 AND n <=12) OR n = 29  then next repeat
>      set the itemDelimiter to numToChar(n)
>      put the number of items in tString -1 into tCount
>      if char -1 of tString is numToChar(n) then add 1 to tCount
>      put n & tab & tCount & cr after tList
>    end repeat
>    put tList into fld "chars"
> end yetAnotherWay
> this was just about twice as fast as "textStr & null" with 3
> megabytes of data.
I agree with Mark on the efficiency of the 'yetAnotherWay' on large text
> Also, shouldn't the count be
> 'the number of items in textStr -1?
As I indicated in a previous post, it depends on what you are trying to
count when you use items a measure.

If the last char of a string is the asciiChar, then the number of items will
be one less than the true count, however, if null is appended, the count
will be equal to the true count.

If Todd's desired solution is to do just a count (but on more than one
asciiChar) then arrays+keys may be the best overall.

In addition, if the locations of the chars are important, that is another
story (eg. hiliting instances)

Good work, Mark.

Jim Ault
Las Vegas

More information about the Use-livecode mailing list