Statalist The Stata Listserver


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

Re: st: RE: Behaviour of -tokenize- shouldn't it drop the parsing character?


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: RE: Behaviour of -tokenize- shouldn't it drop the parsing character?
Date   Thu, 5 Oct 2006 10:26:29 -0400

Another approach uses -foreach-, avoiding -tokenize- altogether:

local test 1 2 3 | 4 5 6| 7 8 | 9
local test : subinstr local test "|" `"" ""', all
local test `""`test'""'
local i 1
foreach element of local test {
 di `"element `i++' is `element'"'
 foreach j of local element {
  di _col(10) "and now do something to each piece: " _c
  di "ln(`j')=" ln(`j')
 }
}

I suppose if you really needed macros named 1, 2, 3,... you could make
them inside the loop.

On 10/4/06, David Elliott <[email protected]> wrote:
Thank you, Scott - I didn't have access to the paper manual and -help
tokenize- omitted that little fact.  Your solution would work with my
first example but for more complex strings like my example: 1 2 3 | 4
5 6| 7 8 | 9 where I want the parse to give me the groups separated by
the "|" as tokens - not the individual items within the groups (that
comes later).  For now I am going to do the following:

local test 1 2 3 | 4 5 6| 7 8 | 9
tokenize `"`test'"' , parse("|")
local i = 1
while `"`i'"' != "" {
   tokenize `"`i'"'
   local j = 1
   while `"`j'"' != "" {
       do something with j
       local ++j
       }
   local ++i
   macro shift
}
*
*   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