count occurrences of a character in a string

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

count occurrences of a character in a string

Brian Moore-7
count occurrences of a character in a string

Hi all-

How might one count the number of times a particular character appears in a string variable?

The string variable is called Qg and commas are the character I want to count.

Im using version 15

Thanks in advance for any suggestions.

Regards,

Brian

PS context of the problem below if you are interested.

& the problem at hand is using an open text field to collect numeric sales data and addressing multiple variations of uses of terms (mil, MM, millions, bil, Billions, etc.) and use of decimals and commas in that context.  And of course to add to the fun data collected in ~20 countries [so the meaning of . and , appearing in a sales figure is opposite in some cases).  Ive addressed most of the issues (and have a  few useful reasonableness checks in other variables), but this last piece has me stuck).

Brian Moore
Market Research Manager
WorldatWork

14040 N. Northsight Blvd.
Scottsdale, AZ. 85260 USA

Tel: 480-304-6832 Fax: 480-483-8352
E-mail: [hidden email]
www.worldatwork.org

UPCOMING CONFERENCES:
Total Rewards 2009 Asia Pacific
Oct. 7-9 | Hong Kong

Total Rewards 2010
May 16-19  |  Dallas / Ft.Worth, TX

Reply | Threaded
Open this post in threaded view
|

Re: count occurrences of a character in a string

Ruben Geert van den Berg
Dear Brian,
 
Maybe something like this?
 
data list free/var(a20).
begin data
'al,mos,t' 'week,,,end' 'lots' 'of beer' 'co,m,i,ng m,,y way'
end data.
 
comp commas=0.
loop #i=1 to length(var).
do if sub(var,#i,1) eq ','.
comp commas=commas+1.
end if.
end loop.
exe.

HTH,
 
Ruben van den Berg


 



 

Date: Thu, 10 Sep 2009 14:38:02 -0700
From: [hidden email]
Subject: count occurrences of a character in a string
To: [hidden email]

Hi all-

How might one count the number of times a particular character appears in a string variable?

The string variable is called Qg and commas are the character I want to count.

Im using version 15

Thanks in advance for any suggestions.

Regards,

Brian

PS context of the problem below if you are interested.

& the problem at hand is using an open text field to collect numeric sales data and addressing multiple variations of uses of terms (mil, MM, millions, bil, Billions, etc.) and use of decimals and commas in that context.  And of course to add to the fun data collected in ~20 countries [so the meaning of . and , appearing in a sales figure is opposite in some cases).  Ive addressed most of the issues (and have a  few useful reasonableness checks in other variables), but this last piece has me stuck).

Brian Moore
Market Research Manager
WorldatWork

14040 N. Northsight Blvd.
Scottsdale, AZ. 85260 USA

Tel: 480-304-6832 Fax: 480-483-8352
E-mail: [hidden email]
www.worldatwork.org

UPCOMING CONFERENCES:
Total Rewards 2009 Asia Pacific
Oct. 7-9 | Hong Kong

Total Rewards 2010
May 16-19  |  Dallas / Ft.Worth, TX



Express yourself instantly with MSN Messenger! MSN Messenger