Counting Chars by ASCI Values

Alex Tweedly alex at tweedly.net
Wed Mar 1 03:54:54 EST 2006


Ken Ray wrote:

>On 2/28/06 3:57 PM, "Jonathan Lynch" <jonathandlynch at gmail.com> wrote:
>
>  
>
>>I like the idea of using itemdel as a means of counting - very clever...
>>
>>I was thinking there must be some other immediate means of counting?
>>    
>>
>
>How about this one?
>
>function CountChars pString,pChar
>  replace space with null in pString
>  replace pChar with space in pString
>  return ((the number of words of pString)-1)
>end CountChars
>
>  
>
That doesn't work because a sequence of consecutive spaces doesn't cause 
intermediate null words - the sequence is a single word delimiter. So if 
you tried to count the ":"s in "asd:::fgh" you'd replace it to "asd   
fgh" - which word-counting says is the same as "asd:fgh".

It would also get the wrong answer if there are quotes in the string 
forming a quoted word. I think it requires a space immediately before 
the first quote for ti to be considered the start of a quoted word - but 
once it does form a quoted word, internal spaces are ignored, as is the 
lack of a space after the second quote.

string             word count
-------            ---------- 
asdfgh             1 as you wanted
asd"extra"fgh      1 again
asd "extra"fgh     3 !!
asd "extra" fgh    3 again
asd "extra"   fgh  3 again


-- 
Alex Tweedly       http://www.tweedly.net

-------------- next part --------------
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.375 / Virus Database: 268.1.1/270 - Release Date: 27/02/2006


More information about the use-livecode mailing list