FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour How to find out how many times a string is in a variable
Posts: 598
Joined: Tue Apr 15, 2008 04:51 PM
How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 12:19 AM

Is there a function that can count the number of times the same string shows up in a variable
for example cVariable := "user1/temp/temp"
I Need to count how many times temp shows up in cVariable.

Thanks for any help.

Thank you

Harvey
Posts: 4043
Joined: Wed Dec 19, 2007 06:40 PM
Re: How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 01:34 AM
Hello Harvey,

msgalert( hb_tokenCount( "1;2;3;4;", ";" ) -1 ) // Result 4
msgalert( hb_tokenCount( "This is a test", " " ) -1 ) // Result 3
msgalert( hb_tokenCount( "use -1 for result", " " ) -1 ) // Result 3
msgalert( hb_tokenCount( "user1/temp/temp", "temp" ) -1 ) // Result 2

Best Regards
Uwe :-)
Since 1995 ( the first release of FW 1.9 )

i work with FW.

If you have any questions about special functions, maybe i can help.
Posts: 598
Joined: Tue Apr 15, 2008 04:51 PM
Re: How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 01:44 AM

works great. Thanks for the quick esponse. :D

Thank you

Harvey
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 02:42 AM

Uwe,

I see the -1. So it really returns one more than the token count? Is this a bug?

Maybe we should create a better function:

function TokenCount( cString, cToken )
return ( hb_tokenCount( cString, cToken) -1 )

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 598
Joined: Tue Apr 15, 2008 04:51 PM
Re: How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 02:44 AM

are you saying when it returns 1 it should be 0 and 2 should be 1. The behavior I experenced was just that.

Thank you

Harvey
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 06:06 AM
James Bott wrote:Uwe,

I see the -1. So it really returns one more than the token count? Is this a bug?

Maybe we should create a better function:

function TokenCount( cString, cToken )
return ( hb_tokenCount( cString, cToken) -1 )

James

Mr. James
Please look at the function again. There is no bug at all.
The function hb_tokenCount( "This is a test", " " ) returns the number of tokens with " " (space) as separator. The function returns 4 meaning that there are 4 tokens, viz., "This", "is", "a", "test". The result is correct. But if we want to know how many times the separator is there, it is naturally the number of tokens minus one.
There is nothing wrong with the function at all, which returns the number of tokens correctly. If we want to use the same function for counting number of times the separator occurs we need to subtract one from the number of tokens.
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 06:15 AM
To count the number of times a substring occurs in a string, there is another function
Code (fw): Select all Collapse
Occurs( <cSubStr>, <cString> ) --> nCount

and also
Code (fw): Select all Collapse
NumAt( <cSubStr>, <cString> ) --> nCount // ct.lib
Regards



G. N. Rao.

Hyderabad, India
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 07:24 AM

Rao,

Thanks for the explaination about hb_tokenCount(). However, I still don't understand this example:

msgalert( hb_tokenCount( "user1/temp/temp", "temp" ) -1 ) // Result 2

If we are looking for the number of occurances of "temp" shouldn't hb_tokenCount() return 2? Then the above should return 1, correct?

The Occurs() function seems to be the better and less confusing choice.

Regards,
James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 10:43 AM
msgalert( hb_tokenCount( "user1/temp/temp", "temp" ) -1 ) // Result 2

If we are looking for the number of occurances of "temp" shouldn't hb_tokenCount() return 2? Then the above should return 1, correct?


The actual purpose of HB_TokenCount() is to get the count of number of tokens separated by the specified separator, but not to count the number of occurances of the separator itself. Here the programmer intends to use a function created for a different purpose to get what he wants. He is using the general principle that number of occurances of a separator is always number or tokens separated by it less 1. Finally he is deriving what he wants. In the above case, the separator is "temp". The tokens separated by "temp" are "user1/", "/" and "", which are three. Now rest is arithmetics.



The Occurs() function seems to be the better and less confusing choice.

Yes, occurs() is a direct function counting the number of occurances of a substring. This gives a direct count without the additional work of counting tokens and subtracting by one.

The other function NumAt(...) is a very old function that many clipper programmers have been quite familiar with since the olden days of Clipper Tools.
Regards



G. N. Rao.

Hyderabad, India
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: How to find out how many times a string is in a variable
Posted: Thu Feb 03, 2011 03:20 PM
Rao,

The tokens separated by "temp" are "user1/", "/" and "",


I guess this is what was confusing me. I wouldn't think that "" counts as a token. It doesn't seem logical to me to count nil as a token. It seems like it is returning the number of tokens plus 1.

I am not arguing with you (the function does what it does), just stating why it seems confusing.

Regards,
James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10

Continue the discussion