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.

Is it possible to conveniently direct the output of consecutive Print statements to the same output without actually accumulating the output in a temporary string/variable.

This would be useful in cases where there are a lot of small outputs say like in the example below:

For[i=1,i<=1000,i++,
Print[i," "];
]

By default each of the Print statement produces a new output cell, which is not desirable. I would prefer a C-style dump-to-terminal output, where one just prints i with a space

share|improve this question
1  
Something like this or you want to affect only Print? –  Kuba Aug 20 '13 at 17:16
    
What about PrintTemporary? –  Yves Klett Aug 20 '13 at 20:02
    
    
Tangentially related: "Is it possible to Print expressions in reverse order?" –  Alexey Popkov Aug 21 '13 at 1:56
    
@AlexeyPopkov : Thanks for the links. The key takeaway is WriteString["stdout",.. –  my account_ram Aug 21 '13 at 7:57

2 Answers 2

up vote 4 down vote accepted
For[i = 1, i <= 10, i++, WriteString["stdout", i, " "];]
share|improve this answer
    
What is the easiest way to get the the output to line wrap to window width? –  Mr.Wizard Aug 20 '13 at 18:47
    
Simple and clean. Exactly What I was looking for! –  my account_ram Aug 21 '13 at 7:57
    
@Mr.Wizard You could either change the Stylesheet or, the hard way, do: For[i=1, If[i===1,(CurrentValue[#,PageWidth]=WindowWidth)&/@Cells[CellStyle->"Print"]]; i<=1000,i++,WriteString["stdout",i," "];] –  Rolf Mertig Aug 21 '13 at 18:42
    
Well I don't have Cells in v7. How would I use the style sheets? Could it be done without affecting anything else (only the custom print function)? –  Mr.Wizard Aug 21 '13 at 18:45
1  
You have to change the Print style: set PageWidth->WindowWidth. I forgot how to do this at the notebook level. But you could create a custom stylesheet I guess. I gave up on style sheets a long time ago and just use the default one. –  Rolf Mertig Aug 21 '13 at 18:48

I would manipulate the front end as the code is being run from the front end

For[i = 1, i <= 1000, i++, Print[i, " "];];
SelectionMove[EvaluationCell[], Next, CellGroup];
FrontEndExecute[FrontEndToken["CellMerge"]]

Try the above code. I am at a loss as to what you will do next with the merged cell.

share|improve this answer
1  
This can simplified a hair by replacing FrontEndExecute[FrontEndToken["CellMerge"]] with FrontEndTokenExecute["CellMerge"]. –  m_goldberg Aug 20 '13 at 23:32
    
@Hans : I have a long-running calculation on a comp which is "unstable" - So i need it to save the outputs peridodically (AutoSave -> True) and want the output to be as compact as possible (which is why I did not want the newline to be automatically added). Thanks for your solution. I could use it in some other case! :) –  my account_ram Aug 21 '13 at 7:58

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.