Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: Trying to do inverse of char(n) without Mata ascii()


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: Trying to do inverse of char(n) without Mata ascii()
Date   Sun, 11 Jun 2006 16:20:02 +0100

A second take on this can exploit a simple 
principle. While Mike's code loops over all 
the possibilities to create a composite 
"dictionary" string containing all the 
characters, almost always you need to do 
less than that. You just need to search until 
what you find what you want, and then
stop. 

For example, 

local i = 1
while char(`++i') != "a" {
* 
}
di `i'

or 

local i = 1
while char(`i') != "a" {
	local ++i
}  
di `i' 

This doesn't work so easily 
with ` , and you wouldn't want 
to do it repeatedly, so -- without 
much checking -- here is another go. 

------------------------- charfind.ado 
*! NJC 1.0.0 11 June 2006
program charfind
	version 8.2
	if `"`0''"' == `"`=char(96)''"' {
		di as res "96"
		exit 0 
	}
	else {
		local i = 1 
		while char(`i') != `"`0'"' {
			local ++i
			if `i' == 256 Goerror 
		}
		di as res "`i'"
	}
end 

program Goerror 
	di as err "invalid character"
	exit 498 
end 
-----------------------------

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

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Nick Cox
> Sent: 10 June 2006 20:16
> To: statalist@hsphsun2.harvard.edu
> Subject: st: RE: Trying to do inverse of char(n) without Mata ascii()
> 
> 
> A problem with this is likely to be that the tab 
> character maps to multiple spaces, so messing up
> your counting. 
> 
> But in terms of your immediate question, look 
> again at 
> 
> strpos(`"`charorder'""', "a")
> 
> The first five quotes read off (d = double, s = single, q = quote) 
> 	`"          left compound d.q. 
> 	`		left s.q. 
> 	'           right s.q. 
> 	"	      d.q. 
> 	"'          right compound d.q. 
> and are thus, as Stata says, not balanced. 
> 
> Nick 
> n.j.cox@durham.ac.uk 
> 
> Mike Lacy
>  
> > I was trying implement a code fragment to give the number 
> of a given 
> > character, i.e., the inverse of char(n) without using the 
> Mata ascii 
> > function. (One of my reasons for doing it is that I am 
> annoyed by the 
> > idea that this is not simply an ordinary function in Stata.)  So, I 
> > tried the following:
> > 
> > *Construct a list of all the characters in order
> > local charorder = ""
> > forval i = 1/255 {
> >    local c = char(`i')
> >    if `i' ~= 96 {  /* avoid the pesky open-quote */
> >       local charorder = `"`charorder'`c'"'
> >    }
> >    else {   /* stick in a stand in */
> >       local charorder = `"`charorder'zzz"'
> >    }
> > }
> > local charorder = subinstr(`"`charorder'"', "zzz", "`",1)
> > *
> > di " The whole list:   " `"`charorder""'
> > * try finding something
> > di "Letter a is found at ", strpos(`"`charorder'""', "a")
> > 
> > While there may well be a more elegant way to get the open-quote 
> > character into a macro that also contains " and ', what I am most 
> > stuck with is the very last line, in which I tried to use strpos to 
> > find the position of a
> > sample character, and the response is "too few quote," which 
> > presumably is the interpreter's complaint about 
> > `"`charorder'""'  in strpos().
> > Any suggestions here?

*
*   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