Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Parse question


From   n j cox <n.j.cox@durham.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Parse question
Date   Thu, 22 Sep 2005 15:57:50 +0100

I was wondering this the other day but didn't
remember anything.

Given this,

*! NJC 1.0.0 22 Sept 2005
program charcount
version 8
args string char garbage
if `"`char'"' == "" | `"`garbage'"' != "" error 198

local c = 0
forval i = 1/`= length(`"`string'"')' {
if substr(`"`string'"',`i',1) == `"`char'"' local ++c
}

c_local count = `c'
end

you should fire at it a quoted string and a quoted character,
e.g.

charcount "`loc'" "a"

and it will insinuate a local macro -count- in your vicinity
with the answer.

di `count'

(It should probably be sclass, and I'll attend to that in due course.)

So watch out for your own use of -count-.

There's also no doubt a Mata solution.

Nick
n.j.cox@durham.ac.uk

adiallo5@worldbank.org

Hi,
Is there any way to count the number of occurence of a particular character in a
local or
global macro?
For example:
local loc (a mama baba).
I would like to count the number of occurences of "a" , in this case 5.
Or local loc "There; That; This)
I want to count the number of occurences of ";".
I am writing an ado for a work and I can't figure how to do it.
I have a look at macrolists but there is no mention of that case.


*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index