newbie Comment question

0 views
Skip to first unread message

János

unread,
Mar 20, 2004, 4:07:59 AM3/20/04
to
I have lines and lines of codes. Like

line1;
line2;
line3;

I would like to comment out line2 temporary. How do I do it ? I tried
to look hard in the Book last night, but found nothing.

Related question. If I do not want to insert a text cell to explain
what a line does, how can I add comments to the lines ?

In other languages /*....*/ or // would work, but not here. I do not
know yet how to add text cells, but would like to comment the code I
do.

Thanks ahead,

János
-------------------------------------------------
clear perl code is better than unclear awk code; but NOTHING comes
close to unclear perl code
http://www.faqs.org/faqs/computer-lang/awk/faq/

David Park

unread,
Mar 22, 2004, 5:24:42 AM3/22/04
to
Janos,

Comments in Mathematica are inserted using (* .... *). So you could use...

line1;
(* line2; *)
line3;

However, in my humble opinion, comments in Mathematica are poorly
implemented and, except for commenting out, are almost totally useless. In
package code they cause parsing errors. If you convert an Input Cell from on
Form to another, say from StandardForm to InputForm, the comments are thrown
away.

Weaving some comments into code is a very useful and proper programming
practice. Text cell comments are in no way a substitute for this. Comments
should be considered as part of the proper syntax of all Input cells. They
should not be thrown out when converting between various input Forms. The
parser should handle comments simply by skipping over them.

This is a long standing incorrectly implemented feature of Mathematica.

I would like to point out another, this time good feature of Mathematica
that many are unaware of. It is very convenient to do a calculation with
multiple input lines in one cell. It is also easy to add comments in the
form of Print statement so that you have commented output. You can use the %
and %% expressions quite safely within a single cell. You can use
statement1; and still refer to its output as %. Here is a simple example of
commented output.

Print["Starting equation"]
a x + b == c
Print["Subtract b from both sides"]
# - b & /@ %%
Print["Divide by a"]
#/a & /@ %%

If you close up the Input cell, using Alt-C-R-C, your notebook will have
just the commented output. This is very convenient for displaying
derivations in Mathematica.

David Park
dj...@earthlink.net
http://home.earthlink.net/~djmp/

Bob Hanlon

unread,
Mar 22, 2004, 5:28:51 AM3/22/04
to
In the Master Index type

comments

and you will be referred to sections

2.9.14, 2.9.23, and A.2.6


Bob Hanlon

In article <c3h1lf$bcb$1...@smc.vnet.net>, =?ISO-8859-1?Q?J=E1nos?=

Bill Rowe

unread,
Mar 22, 2004, 5:31:54 AM3/22/04
to
On 3/20/04 at 3:50 AM, janos...@yale.edu (János) wrote:

>I have lines and lines of codes. Like

>line1;
>line2;
>line3;

>I would like to comment out line2 temporary. How do I do it

The comment delimiters in Mathematica are "(*" "*)" pairs. So

line1;
(* line2; *)
line3;

will execute as

line1;
line3;

--
To reply via email subtract one hundred and four

Paul Abbott

unread,
Mar 25, 2004, 5:50:35 AM3/25/04
to
In article <c3h1lf$bcb$1...@smc.vnet.net>, János <janos...@yale.edu>
wrote:

> I have lines and lines of codes. Like
>
> line1;
> line2;
> line3;
>
> I would like to comment out line2 temporary. How do I do it ? I tried
> to look hard in the Book last night, but found nothing.

If the three inputs are in separate cells (IMHO, the right way to enter
Mathematica code) then you can simply make line2 inactive by selecting
the cell bracket and using the Menus to uncheck the "Cell Evaluatable"
attribute:

Cell | Cell Properties | Cell Evaluatable

(The Evaluatable->False option is attached to the Cell.)

> Related question. If I do not want to insert a text cell to explain
> what a line does, how can I add comments to the lines ?

But why not enter a text cell? Mathematica Notebooks are designed to mix
text and code (and graphics and sound ...). Using

File | Save As Special... | Package Format

only Initialization cells in Notebooks are saved when the Package is
generated so it is completely natural to write Packages as Notebooks,
with extensive documentation and comments.

For an excellent example of formatted Notebook have a look at the
Formatted Source Code for the Notation package by pasting the following
cell into Mathematica and following the link that is generated:

Cell[BoxData[ FormBox[ ButtonBox[
RowBox[{"...", "Formatted", " ", "Source", " ", "Code"}],
ButtonStyle->"DemosLink"], TraditionalForm]], "Input"]

Cheers,
Paul

--
Paul Abbott Phone: +61 8 9380 2734
School of Physics, M013 Fax: +61 8 9380 1014
The University of Western Australia (CRICOS Provider No 00126G)
35 Stirling Highway
Crawley WA 6009 mailto:pa...@physics.uwa.edu.au
AUSTRALIA http://physics.uwa.edu.au/~paul

Reply all
Reply to author
Forward
0 new messages