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:

Print[i," "];

By default each of the Print statements 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.

For[i = 1, i <= 10, i++, WriteString["stdout", i, " "];]
| improve this answer | |
  • $\begingroup$ What is the easiest way to get the the output to line wrap to window width? $\endgroup$ – Mr.Wizard Aug 20 '13 at 18:47
  • $\begingroup$ Simple and clean. Exactly What I was looking for! $\endgroup$ – my account_ram Aug 21 '13 at 7:57
  • $\begingroup$ @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," "];] $\endgroup$ – Rolf Mertig Aug 21 '13 at 18:42
  • $\begingroup$ 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)? $\endgroup$ – Mr.Wizard Aug 21 '13 at 18:45
  • 1
    $\begingroup$ 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. $\endgroup$ – 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];

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

| improve this answer | |
  • 1
    $\begingroup$ This can simplified a hair by replacing FrontEndExecute[FrontEndToken["CellMerge"]] with FrontEndTokenExecute["CellMerge"]. $\endgroup$ – m_goldberg Aug 20 '13 at 23:32
  • $\begingroup$ @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! :) $\endgroup$ – my account_ram Aug 21 '13 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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