Page 1 of 3

Word Count

Posted: Sun Oct 12, 2003 1:37 am
by Summers
Is there an easy way to count words in a string? In Arexx I believe it was something like:

words("some string here")

Posted: Sun Oct 12, 2003 2:27 am
by Karbon
You could use this to count whitespace characters :

Code: Select all

Procedure.l CountChars(a.s,s.s)
  !MOV edi,dword[esp]    ;pointer to the first character in string (first function parameter)
  ;firstly we must found the lenght of the string:
  !CLD              ;clear DF (Direction Flag). Could be not necessary.
  !XOR al,al       ;set NULL character in AL register
  !XOR ebx,ebx  ;init counter
  !MOV ecx,ebx    ;lets set 4294967295 ($FFFFFFFF) characters maximum
  !DEC ecx
  !REPNZ scasb    ;repeat comparing AL CPU register content with [edi]
  !JECXZ go    ;if NULL byte is not found within those 4294967295 characters then exit giving 0
  !NOT ecx     ;else, some adjusts. Now we have the lenght at ecx register
  !MOV edi,dword[esp]     ;point again to the first character in string (first function parameter)
  !MOV eax,dword[esp+4]
  !MOV al,byte[eax]    ;al=character to find
  !@@:REPNZ scasb   ;repeat comparing AL CPU register content with [edi]
  !JECXZ go     ;until ecx value is reached
  !INC ebx       ;or a match is found
  !JMP @r       ;continue comparing next character
  !go:MOV eax,ebx   ;output the matches counter
  ProcedureReturn
EndProcedure

Posted: Sun Oct 12, 2003 5:27 am
by sec
:)

Code: Select all

s.s="abc def a re era " ;s(len(s)) must equal = " " space
i = 0
count = 0
Repeat
 i=FindString(s," ",i+1)
 If i>0 
  count=count+1
 EndIf
Until i=0

Debug count

Posted: Sun Oct 12, 2003 5:48 am
by wayne1
To search the forums:

(1) Click the Search link at the top-right of this window.
(2) Type in your keyword(s) and click the Search button. :wink:

viewtopic.php?t=3603&highlight=countwords

Posted: Sun Oct 12, 2003 9:17 am
by sec
ahh,
how use ASC() to sum of a string
sum=sum+ asc(mid(string,1,1)) <- don't do
??

Posted: Sun Oct 12, 2003 12:11 pm
by Summers
wayne1 wrote:To search the forums:

(1) Click the Search link at the top-right of this window.
(2) Type in your keyword(s) and click the Search button. :wink:

viewtopic.php?t=3603&highlight=countwords
No offence, but this has the worst search page ever. I've been on a lot of forums, yet whatever I do this brings a lot of results. Mostly rubbish.

Posted: Sun Oct 12, 2003 3:27 pm
by Karbon
No offense, but you haven't used a lot of forum software - have you? PHPbb's searching is pretty darn good as far as forum searches go ...

Posted: Sun Oct 12, 2003 4:09 pm
by Tomio
sec wrote::)

Code: Select all

s.s="abc def a re era " ;s(len(s)) must equal = " " space
i = 0
count = 0
Repeat
 i=FindString(s," ",i+1)
 If i>0 
  count=count+1
 EndIf
Until i=0

Debug count
In a test I found that in larger strings (50bytes+...) FindString is much slower than a loop with: if PeekB(i) = ' ': wordcount+1:endif

Because FindString does not remember it's last call, it always has too start from the string's beginning.

../tomio

Posted: Sun Oct 12, 2003 4:16 pm
by Summers
Karbon wrote:No offense, but I don't think you know what you're talking about :-)
Cheap reply. Seriously, re-read my post.

Posted: Sun Oct 12, 2003 4:36 pm
by Karbon
I was only joking and modified that post right after you replied!

Seriously, PHPbb's searching isn't that bad.

Click search, select "Search for all terms" type in "count words"

Bam - the "Challenge: Fastest Way To Parse And Count Words?" thread comes up. Don't know what more you could ask for :-)

It's not perfect, but the random free-form input of a forum isn't really the easiest thing to do context searching on...

Posted: Sun Oct 12, 2003 4:38 pm
by Pupil
If you don't use the Option 'Search for ANY terms...' you get a better result when searching. Maybe this is of interest: viewtopic.php?t=2450&highlight=count+words

Posted: Sun Oct 12, 2003 8:40 pm
by ebs
Here's a little trick I learned for counting characters:

Code: Select all

s.s="abc def a re era "

NoSpaces.s = RemoveString(s, " ")
Debug Len(s) - Len(NoSpaces)
Eric

Posted: Mon Oct 13, 2003 10:31 am
by Fred
May be a CountString() command is needed ? :)

Posted: Mon Oct 13, 2003 1:37 pm
by ebs
Fred,

Would you consider adding something like the VB "Split" function? It fills an array with string fields separated by a specified delimiter. It's very handy for parsing words in a sentence and comma or tab delimited files.

Regards,
Eric

Posted: Mon Oct 13, 2003 3:17 pm
by Karbon
A built in CountChars() would be good - that way you could count the number of any character you wanted in a string..