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

RE: st: RE: file wrote single opening quote


From   "Nick Winter" <nwinter@policystudies.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: file wrote single opening quote
Date   Mon, 18 Nov 2002 10:59:26 -0500

> -----Original Message-----
> From: Ulrich Kohler [mailto:ukohler@sowi.uni-mannheim.de] 
> Sent: Monday, November 18, 2002 11:50 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: RE: file wrote single opening quote
> 
> 
> Nick Cox  wrote
> > Ulrich Kohler
> > > Is there a way to wrote a single opening quote to a file
> > > with -file wrote-?
> > >
> > > In a program I have the command
> > >
> > >    file write texfile `"`line'"'
> > >
> > > which wrotes the contents of the local macro `line' to the
> > > file handle
> > > "texfile". The command works well except for lines which
> > > contains either a
> > > local macro name (like `a') , or a single opening quote. In
> > > the first case
> > > Stata expands the local macro name to the contents of the
> > > local macro. But I
> > > want to have the local macro name (`a') in my file handle.
> > > In the second case
> > > I got an error message because Stata thinks that there
> > > should be a valid
> > > local macro name, which is not there.
> 
> > use _char(96)
> 
> 
> This don't works for me. I got the macro `line' from -file 
> read-. The file I 
> read may contain lines with single quotes and/or macro names. 
> To wrote this 
> line (after some selections and/or replacements) to another 
> file I use file 
> write. To use the _char(96)-solution I need to substitute 
> single opening 
> quotes with something like:
> 
> local line: subinstr local line `"`"' `"_char(96)"'
> 
> which does not work. 
> 
> Here is a (very) simple example of the problem. Consider the 
> following sequence: 
> 
>   file open texfile using saving.tex, replace write text
>   file open dofile using using.do, read text
>   file read dofile line 
>   while r(eof)==0 {
>     file write texfile _n `"`line'"'
>     file read dofile line
>   }
>   file close dofile
>   file close texfile
> 
> This should "copy" the file using.do to the file "saving.tex" 
> line by line. 
> It works---but not for lines with local macros... 
> 

In general, my basic reaction to the recent threads on this sort of
thing is that Stata is not perl, and so is not really the greatest tool
for this sort of thing.  The reason that local macros are so powerful in
Stata is that they are parsed at a *very* low level, which makes it hard
to read and write arbitrary strings containing local macro references.

That said, if you really want to go down this path, you will probably
have to do the writing (and maybe the reading too) at a lower level,
using -file-'s binary read/write capabilities.

My -log2do2- program does this for tranlating .log files into .do files.


	ssc desc log2do2

for that program, which might be helpful in designing what you're doing.

Nick Winter
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index