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

RE: st: Long arguments passed to programs


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Long arguments passed to programs
Date   Thu, 8 Jan 2004 14:14:23 -0000

Michael's postings make it fairly clear that he doesn't want
to use a variable. Using a macro would be fine, but
invisible to Michael's program as the macro name would be
substituted by its contents before being passed to his
program. Thus the issue is how to process the input,
not how the input is supplied.

Incidentally, his problem reminds me of two
long-standing requests of mine on -tokenize-.

1. If I say -tokenize- on commas, I usually
don't want the commas themselves as tokens; they
are separators and usually of no further interest.

That is,

. tokenize "a b,c d,e" , parse(",")

returns as tokens

a b
,
c d
,
e

whereas I typically just want

a b
c d
e

It is naturally possible to set
up a filter downstream to take out
or ignore the commas, but I'd
rather that this was optionally available
through -tokenize-.

2. Sometimes, it helps to know how many
tokens were produced. For example, -mac shift-ing
through a very long list of tokens of unknown
cardinality is inefficient
and it is a little awkward in any case.

Much of this in fact seems to be adoable
while I'm waiting hopefully.

program tokenize2, rclass
*! NJC 1.0.0 8 Jan 2004
	version 8
	gettoken what 0 : 0  , parse(",")
	syntax [, Parse(str)]
	tokenize `"`what'"' `0'

	forval i = 1/`=length("`parse'")' {
		local p = substr("`parse'",`i',1)
		local newparse "`newparse' `p'"
	}
	local parse `newparse'

	local i = 0
	while `"`1'"' != "" {
		local OK 1
		foreach c of local parse {
			if `"`1'"' == `"`c'"' {
				local 1
				local OK 0
				continue, break
			}
		}
		if `"`1'"' != "" & `OK' {
			local new_`++i' `"`1'"'
		}
		mac shift
	}

	forval j = 1/`i' {
		di "{txt}_`j':   {col 17}{res}`new_`j''"
		c_local `j' `"`new_`j''"'
	}

	return local ntokens `i'
end

For example,

tokenize2 "a b,c d,e", parse(,)

leaves behind just four local macros
in the calling program's space.

Notes:

1. Not tested heavily!

2. This leaves in its wake local macros named
1 to `r(ntokens)' and does not zap locals named
by higher integers.

3. -tokenize2- displays the tokens it
produces, which you might want to suppress.

Nick
[email protected]

R. Allan Reese

> I may be missing something here, but it strikes me as odd to
> routinely pass a very long string as an actual parameter.
> Surely you
> put the string in a variable or a macro and pass the *name* of that
> object as the parameter.

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index