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.


Mark

On 1 Mar 2006, at 15:25, Jim Ault wrote:

On 3/1/06 5:08 AM, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:


In a message dated 2/28/06 6:20:02 PM, Todd Geist
<[EMAIL PROTECTED]> 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:


Actually, my original post used the following technique:

set the itemDel to "π"
get the number of items in (textStr & null)

which does not require a repeat loop or replace or a function or
establishing more variables.

One post was done to show the precaution of counting any container for
words, items, lines where the last to be counted was empty.

The best implementation would be either:
-- method 1 inline
put the itemDel into prevDel
set the itemDel to asciiChar
get the number of items in (textStr & null)
set the itemDel to prevDel

---method 2 function
function getCharCount asciiChar
   set the itemDel to asciiChar
   return  the number of items in (textStr & null)
end getCharCount

--and now the itemDel does not have to reset
put getCharCount("π") into howMany

So many ways, so many choices :-)

Jim Ault
Las Vegas


_______________________________________________
use-revolution mailing list
[email protected]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution

_______________________________________________
use-revolution mailing list
[email protected]
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution

Reply via email to