How to find out how many times a string is in a variable

How to find out how many times a string is in a variable

Postby hag » 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
hag
 
Posts: 598
Joined: Tue Apr 15, 2008 4:51 pm
Location: LOs Angeles, California

Re: How to find out how many times a string is in a variable

Postby ukoenig » Thu Feb 03, 2011 1: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 :lol:
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.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: How to find out how many times a string is in a variable

Postby hag » Thu Feb 03, 2011 1:44 am

works great. Thanks for the quick esponse. :D
Thank you
Harvey
hag
 
Posts: 598
Joined: Tue Apr 15, 2008 4:51 pm
Location: LOs Angeles, California

Re: How to find out how many times a string is in a variable

Postby James Bott » Thu Feb 03, 2011 2: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
Last edited by James Bott on Thu Feb 03, 2011 2:45 am, edited 1 time in total.
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: How to find out how many times a string is in a variable

Postby hag » Thu Feb 03, 2011 2: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
hag
 
Posts: 598
Joined: Tue Apr 15, 2008 4:51 pm
Location: LOs Angeles, California

Re: How to find out how many times a string is in a variable

Postby nageswaragunupudi » Thu Feb 03, 2011 6: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
User avatar
nageswaragunupudi
 
Posts: 10631
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: How to find out how many times a string is in a variable

Postby nageswaragunupudi » Thu Feb 03, 2011 6:15 am

To count the number of times a substring occurs in a string, there is another function
Code: Select all  Expand view
Occurs( <cSubStr>, <cString> ) --> nCount

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

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10631
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: How to find out how many times a string is in a variable

Postby James Bott » Thu Feb 03, 2011 7: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: How to find out how many times a string is in a variable

Postby nageswaragunupudi » 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
User avatar
nageswaragunupudi
 
Posts: 10631
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: How to find out how many times a string is in a variable

Postby James Bott » Thu Feb 03, 2011 3: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 67 guests