6
$\begingroup$

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 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.

$\endgroup$
5

2 Answers 2

5
$\begingroup$
For[i = 1, i <= 10, i++, WriteString["stdout", i, " "];] 
$\endgroup$
5
  • $\begingroup$ What is the easiest way to get the the output to line wrap to window width? $\endgroup$ Commented Aug 20, 2013 at 18:47
  • $\begingroup$ Simple and clean. Exactly What I was looking for! $\endgroup$ Commented Aug 21, 2013 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$ Commented Aug 21, 2013 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$ Commented Aug 21, 2013 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$ Commented Aug 21, 2013 at 18:48
3
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ This can simplified a hair by replacing FrontEndExecute[FrontEndToken["CellMerge"]] with FrontEndTokenExecute["CellMerge"]. $\endgroup$ Commented Aug 20, 2013 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$ Commented Aug 21, 2013 at 7:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.