Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Can please someone explain to me the diffrent behavior of this code

(6 - 3);
(5 - 3);
(4 - 3);
{%%%, %%, %}

Out= {3, 2, 1}

and this one

a;
b;
c;
(6 - 3);(*comment*)
(5 - 3);(*comment*)
(4 - 3);(*comment*)
{%%%, %%, %}

Out= {a, b, c}

? I thought it should be the same. Do the comments block the outputs to be used with %?

EDIT:

I found a working case ... even if one adds spaces between the comments.

a;
b;
c;
(6 - 3);
(* comment *)
(5 - 3);
(* comment *)
(4 - 3);
(* comment *)
{%%%, %%, %}

Out= {3, 2, 1}
share|improve this question
    
Run the second case with a new kernel. The output is {Out[0],3,2}. The {3,2,1} in the second case is the output of the first case. Not sure what is happening but your two cases are colliding. –  Ymareth Jan 14 at 10:31
    
@Ymareth Right, but what's about the comments? Without them everything runs fine. Do the comments block the output to be used with %? –  Phab Jan 14 at 10:36
3  
You have some whacky invisible characters in there somehow: when I c&p the second example, it pastes as one line. When I type in manually, works as expected. Whatever the case, it's making MM think the stuff is on one line, however it's displaying, so the %% is the prior whole result, hence the 'stacking' of the results together. –  rasher Jan 14 at 10:46
    
@rasher you're right, but if I type a space between the semicolon and the bracket of the comment, it has the old behaviour. And even if I delete the space again, it stucks to his behaviour. How should one edit his comments with this happening? –  Phab Jan 14 at 10:53
    
@Phab: I've no idea how the post contents got whacked if that's the case here, but regardless, when I type it in manually to my MM it works as expected, and as you'd expect. Perhaps start a fresh MM session, and just type it in again, see if it works like you correctly expect it to? –  rasher Jan 14 at 10:58
show 1 more comment

2 Answers

If you examine the Cell expressions (select the cell and hit ctrl-shift-E) you can see that in the first case the lines are separated by \n whereas in the second case [IndentingNewLine] sneaks in (I pasted the second case and hit return between the lines as it pasted as a single line). It would appear that [IndentingNewLine] doesn't count as an input separator so when the evaluation runs it treats all 3 lines as one, that line ends in a ; so no output is produced.

Manually editing the cell expression to use \n instead produces the expected evaluation.

When I simply paste the second case, the code looks like

a;
b;
c;
(6 - 3);(*comment*)(5 - 3);(*comment*)(4 - 3);(*comment*)
{%%%, %%, %}

but the only lines with a \n at the end are the first 3. I therefore conclude (speculate) that missing \ns are the issue here. Not sure why they occur (or not) though.

share|improve this answer
    
+1 for digging under the covers on this. I suspect a bug in auto-formatting, though turning off a bunch of it had no effect but I didn't exhaust options. OP should file a (possible) bug report w/ WRI. –  rasher Jan 14 at 13:13
    
there s no "\n" in my file here, I just get "[IndentingNewLine]". But I have empty lines between. If I manualy delete them, I'll get the expected result. –  Phab Jan 14 at 13:22
add comment

Comments do block the outputs when using %, but to avoid the weird effect you found adding space after the CompoundExpression (;) I suggest you to put the semicolon after the comment. In this way the comment block % independently of blank spaces.

a;
b;
c;
(6 - 3) (*comment*);
(5 - 3) (*comment*);
(4 - 3) (*comment*);
{Out[-3], Out[-2], Out[-1]}

{a,b,c}
share|improve this answer
1  
Well done! That's an answer I like! But the explanation still doesn't satisfy me. –  Phab Jan 14 at 12:12
    
@Phab In fact, I do not understand why spaces and lines affect the % command. But I suspect that it is some type of bug, and in this case there is a satisfying explanation :-) –  sam84 Jan 14 at 12:43
    
So, note for future notebooks: comments in an extra line, or CompoundExpression(;) after the comment. –  Phab Jan 14 at 12:45
1  
@Phab A better idea would be to avoid using %%%, %%, etc. A notebook is not necessarily sequential... evaluating a cell at the end of the notebook and then evaluating % at the top will return the output from the last cell. If you revisit your notebook a few months later, you won't remember what that % refers to. I'm not saying that you should avoid it completely... it can be handy, but it would be better (in the long run) if you didn't rely on it. :) –  rm -rf Jan 14 at 15:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.