Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Two-word commands with gettoken


From   wgould@stata.com (William Gould, StataCorp LP)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Two-word commands with gettoken
Date   Mon, 09 Mar 2009 08:18:50 -0500

Augusto Cadenas <aug.cadenas@googlemail.com> asked, 

> I have a question about -gettoken- and programming in Stata. The stata
> help file suggests that -gettoken- can be used to create a two-word
> command. This is the example that is given:
> 
> [...]

What follows, basically, is 

        program define mycmd
                gettoken subcmd 0 : 0
                if ("`subcmd'" == "reg" | "`subcmd'"=="areg") {
                        myareg `0'
                }
                else if ...
        end

Obviously, subroutine myareg will at somet point need to know whether 
`subcmd' was reg or areg.

The answer has already been given, which is to put 0 back together:

        program define mycmd
                gettoken subcmd 0 : 0
                local 0 `subcmd' 0

                if ("`subcmd'" == "reg" | "`subcmd'"=="areg") {
                        myareg `0'
                }
                else if ...
        end

Then, in myareg, Augusto can use -gettoken- to take 0 back apart again.

I don't much like that solution because it is so inelegant.  Better, 
in my mind, is 

        program define mycmd
                gettoken subcmd 0 : 0

                if ("`subcmd'" == "reg" | "`subcmd'"=="areg") {
                        myareg `subcmd' `0'
                }
                else if ...
        end

The only thing better about the above is that I think it is more obvious tht
-myareg- will be receiving subcmd *AND* 0.

By the way, rather than coding 

               gettoken subcmd 0 : 0

Augusto could have coded
                       
               gettoken subcmd : 0

and then the issue would never have come up.  See [P] gettoken.
In this second form, subcmd is filled in with the first token of 
`0', but `0' remains unchanged.

I have one more minor issue.  However Augusto codes his solution, he 
should specify -gettoken-'s -parse()- option, 

               gettoken subcmd 0 : 0, parse(" ,")

Like I said, it's a minor point, but it's sometimes important.
By default, if you do not specify option -parse()-, results are as 
if you specified -parse(" ")-, which is to say, tokens are defined 
by blanks.  I am suggesting that comma be used a separater character, too.
Say the user typed 

               mycmd areg, ...

As Augusto now has things, `subcmd' will be "areg," and that is not what 
Augusto wants.  With -parse(" ,") specified, `subcmd' will be "areg"
and 0 will be redefined as ", ...", which is what Augusto wants.

-- Bill
wgould@stata.com
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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