6

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.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
my account_ram
  • 2,158
  • 11
  • 25

2 Answers2

5
For[i = 1, i <= 10, i++, WriteString["stdout", i, " "];]
Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76
  • 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 07: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
3

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Hans
  • 946
  • 6
  • 8
  • 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 07:58