Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Why do `test' and a`test' make a difference?


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Why do `test' and a`test' make a difference?
Date   Mon, 11 Feb 2013 15:36:38 +0000

I am not aware of a single source that documents in one place how
Stata parses command lines. My wild idea is that it would be too
complicated or too technical to be of much use, even to programmers.

My own main rule is too elementary and too homespun to be much use,
namely that it's best to assume that Stata is right by its own rules
and that misunderstandings have to be treated by trying to see what
you typed from her point of view, token by token, left to right, top
down, and very slowly.

Nick

On Mon, Feb 11, 2013 at 3:01 PM, Jeph Herrin <stata@spandrel.net> wrote:
> Thanks Daniel and Nick,
>
> I can usually work out why I get what I get, or at least get what I want,
> but do find that my most common programming errors occur because macros do
> not behave as I anticipate. Clearly, it is a limitation of my intuition, not
> of Stata, because I don't doubt that Stata is consistent; and I also don't
> doubt that if I used Stata for parsing strings as much as I once used Perl
> or awk or sed (ie, many hours a day) I wouldn't be surprised so often.
> Still, that I still get bit by these - and Stata is practically the only
> thing I program these last few years - suggests that the quirkiness Nick
> refers is perhaps a bit overglossed.
>
> cheers,
> Jeph
>
>
>
>
> On 2/11/2013 8:24 AM, Nick Cox wrote:
>>
>> Daniel's excellent, thorough answer is difficult to gloss.
>>
>> Cory's elegant summary "Stata kinda sucks with strings..." is,
>> however, I think a little hard and so some more general comments seem
>> in order.
>>
>> Every programming or command language of substance faces a challenge
>> that once you give specific syntactic meaning to certain characters,
>> then you make it more difficult to use those characters with their
>> standard, customary or literal meanings. Also, you may get bitten by
>> forgetting or not knowing about the difference. (Me too, naturally.)
>>
>> Also, programmers in my experience tend to get accustomed to the
>> particular choices made in a certain language and then are inclined to
>> regard other solutions in other languages as defective or at least
>> strange.
>>
>> Stata is quirky in wanting to interpret `stuff'  as a reference to a
>> local macro named stuff. Extensions of that syntax `:   ' and `= ' may
>> seem equally strange. So, that can get complicated when you want
>> either or both single quotation marks for other purposes.
>>
>> The original syntax for local macro references was fairly ugly
>> %_stuff was a reference to what was defined by
>>
>> mac def _stuff  "some stuff"
>>
>> You can still use that under version control!
>>
>> Nick
>>
>> On Mon, Feb 11, 2013 at 11:53 AM, daniel klein
>> <klein.daniel.81@gmail.com> wrote:
>>>
>>> As for Jeph's request for a systematic account of how word lists are
>>> parsed, I think this is explained in the manuals and is more about
>>> macro expansion than lists of words.
>>>
>>> When you type
>>>
>>> `: word 1 of `test''
>>>
>>> Stata sees
>>>
>>> `: word 1 of Jeph's Example'
>>>
>>> since `: word 1 of Jeph' expands to Jeph, you get
>>>
>>> Jephs Example'
>>>
>>> When you type
>>>
>>> : word 1 of `test'
>>>
>>> Stata sees
>>>
>>> : word 1 of Jeph's Example
>>>
>>> which expands to Jeph's
>>>
>>> When you type
>>>
>>> `"`:word 1 of `test''"'+"[ ]"+`"`:word 2 of `test''"'
>>>
>>> Stata sees
>>>
>>> `"`: word 1 of Jeph's Example'"' + "[]" + `"`: word 2 of Jeph's
>>> Example'"''
>>>
>>> we already coverd the first part of this, so lets take a look on the
>>> second. Since `: word 2 of Jeph' is an empty string teh hole
>>> expression becomes
>>>
>>> `"Jephs Example'[]s Example'"'
>>>
>>>
>>> These outputs might not be what you want, but exactly what you would
>>> expect following Stata's logic of macro expansion.  Thre is nothing
>>> unsensical or mysterious about it.
>>>
>>> Best
>>> Daniel
>>> --
>>> It's getting tripped up by the apostrophe in test since it's a
>>> character with special meaning. Stata kinda sucks with strings...
>>>
>>> Try local test "Jephs Example" and it will be more sensical.
>>>
>>> On Sun, Feb 10, 2013 at 10:10 PM, Jeph Herrin <stata@spandrel.net> wrote:
>>>>
>>>> I'd like a systematic account of how word lists are parsed. For
>>>> instance, I
>>>> have been scratching my head over the output of this:
>>>>
>>>> local test "Jeph's Example"
>>>> local wordA `: word 1 of `test''
>>>> local wordB : word 1 of `test'
>>>> local wordC =`"`:word 1 of `test''"'+"[ ]"+`"`:word 2 of `test''"'
>>>> di "`wordA'"
>>>> di "`wordB'"
>>>> di "`wordC'"
>>
>> *
>>
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>>
>>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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