Statalist


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

Re: st: comment out a number of lines together


From   Steven Samuels <[email protected]>
To   [email protected]
Subject   Re: st: comment out a number of lines together
Date   Thu, 2 Oct 2008 17:04:07 -0400

You are right, Sergiy. It's another of several good catches you've had today! You are saying that "//" in the second line of a multi- line command is viewed as a comment at the end of the previous line, but adding spaces at the end of the previous line does not help; the space must precede the // on the newline. I missed this because, in my example, I failed to add a semicolon after the previous ** comment; the actual comment began with the * and worked.

Thanks for clarifying all of this.

-Steve



*****************CODE BEGINS****************
sysuse auto, clear
#delim ;

********THIS COMMENT WORKS (single line command) ************;

// sum mpg if rep78==1 ;

****************** THIS FAILS (multi-line) ************;
// sum mpg
// if rep78 ==1;


Thanks,

Steve
On Oct 2, 2008, at 4:27 PM, Sergiy Radyakin wrote:


On Thu, Oct 2, 2008 at 4:00 PM, Steven Samuels <[email protected]> wrote:

Sergiy,
-foreach-, or rather, the brackets it requires, must have something to do
with it.

-Steve

*****************CODE BEGINS****************
sysuse auto, clear
#delim ;
*****************No Comments****************
sum mpg
if rep78 ==1 ;
****Putting a space in front of // works, as you stated:
// sum mpg
// if rep78 ==1;

THIS IS A SINGLE LINE OF CODE: <<<<<<

*****// without a preceding space works too:
// sum mpg
// if rep78 ==1;
UNTIL THE SEMICOLUMN.

Try putting a semicolumn right after the column and it breaks immediately.

Sergiy




*****************CODE ENDS****************

On Oct 2, 2008, at 3:38 PM, Sergiy Radyakin wrote:

This is not because of fovalues or foreach. See help comments:
"However, if the // indicator is at the end of a line, it must be
preceded by one or more blanks."

This is quite a unique requirement of Stata. I can't come up with any
other system which would demand anything like that. Adding a space in
the beginning of every line should not change the program, but in this
case in does (and helps to fix the error).

The fact that it works when the delimiter is CR is hence inconsistent
with the documented behavior.

Best regards, Sergiy Radyakin



On Thu, Oct 2, 2008 at 3:27 PM, Steven Samuels <[email protected]>
wrote:


Here's another example where * works and // doesn't. The circumstances:
the
delimiter is ";", and the code block contains a -forvalues- or - foreach-
section. /* and */ around the -foreach- section also works.

-Steve

*****************CODE BEGINS****************
sysuse auto, clear
forvalues i=1/5 {
di "rep78 = " `i'
}

#delim ;

forvalues i=1/5 { ;
di "rep78 = " `i';
};
;

***************** (*) COMMENT WORKS ****************
* forvalues i=1/5 { ;
* di "rep78 = " `i';
* };
* ;

***************** // COMMENT FAILS****************
// forvalues i=1/5 { ;
// di "rep78 = " `i';
// };
// ;
*****************CODE ENDS****************

On Oct 2, 2008, at 12:44 PM, Nick Cox wrote:


Very good catch, Sergiy. Thanks for the correction! I should have
checked about the //.
Conversely, this is another argument for the deprecated *.

Nick
[email protected]

Sergiy Radyakin

Nick,

local c //
will not work in the program because "//" is interpreted as the start
of the comment
it works in the command line though, because "//"-style comments are
prohibited in the command line input. To fix this:
1) open quotes before // like so: local c "//" , or
2) write it down as an expression, e.g. local c="/"+"/"
In any case, Stata fails to recognize "//" as a comment when it comes
as a result of macro expansion. But it works for a star ("*").

// --- begin of file testc.do ---
local c //
display `"`c'"'
local c ="/"+"/"
display `"`c'"'
local c "//"
display `"`c'"'
local c "*"
display `"`c'"'
`c' just nothing
local c "//"
`c' just nothing
// --- end of file testc.do ---

If writing out debugging messages, it may be more comfortable to use
di_g. See: help di_g.

Best regards, Sergiy Radyakin

On Tue, Sep 30, 2008 at 11:28 AM, Nick Cox <[email protected]> wrote:


Another is to prefix lines by e.g.

`c'

Setting

local c *

or

local c //

comments out those lines.

Setting

local c

zaps the macro.

Naturally any definition of the local as "*" or "//" must precede all
uses

that require that interpretation.

The converse does not apply: if the local is not defined, Stata sees
the

line.

More generally, you can define or undefine the macro at will.

I don't especially recommend this method, but it is an alternative
that is
sometimes useful, and it matches practices in many programming
languages.
Nick
[email protected]


Maarten buis wrote:
--- Paul O'Brien <[email protected]> wrote:

at times i need to comment out a number of lines, usually with * and
remove the * later to activate the commands.
is there a simple way to remove and add the * to a number of line at
one
go?
One way to comment out a block of code is to surround them with /*
and
*/ this way you have to remove only these two.
*
*   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/
*
*   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/

*
*   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/
*
*   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/

*
*   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/
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index