Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

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


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: Why do `test' and a`test' make a difference?
Date   Mon, 11 Feb 2013 10:01:37 -0500

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
<[email protected]> 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 <[email protected]> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index