Counting Chars by ASCI Values

Cubist at aol.com Cubist at aol.com
Wed Mar 1 08:08:24 EST 2006


In a message dated 2/28/06 6:20:02 PM, Todd Geist 
<tg.lists at geistinteractive.com> writes:
>I need to count the number of times a Character occurs in text  
>string. The character will be high or Low ASCII.  SO I need to input  
>the value as an ASCII value.
>
>Anybody have any scripts that can get me started?
   The replies you've gotten thus far have used loops to explicitly count the 
occurences of whatever-character-it-is. Here's a different way to do it, 
using the "replace" function, which could be faster:

function HowManyCharXs TheText, ASCIIval
  # TheText is whatever you're interested in counting the characters of
  # ASCIIval is the ASCII value for the character you want to count

  put the length of TheText into Fred
  replace (numToChar (ASCIIval)) with "" in TheText

  return (Fred - the length of TheText)
end HowManyCharXs

   This function uses "the length of" twice for each character-count. If you 
want to count multiple characters at any one time, it should be possible to 
speed things up a trifle by eliminating nearly half of the needed "the length 
of"s:

function CountManyChars TheText, ASCIIvals
  # TheText is whatever you're interested in counting the characters of
  # ASCIIvals is a comma-delimited list of ASCII values for all characters 
you want to count

  put the length of TheText into Fred
  put "" into Rezultz

  repeat for each item II in ASCIIvals
    replace (numToChar (II)) with "" in TheText
    put the length of TheText into George
    put (Fred - George) into item (1 + the number of items in Rezultz) of 
Rezultz
    put George into Fred
  end repeat

  return Rezultz
end CountManyChars

   Hope this helps...



More information about the Use-livecode mailing list