3
$\begingroup$

I have a loop that was printing out results, and now I'm left with many cells in different sections and I need to combine them (the values in them) into a list I can work with. For example, applied to enter image description here

it would give me

{1, 2, 3, 4, ..., 4000}

Clarifications:

  • The results I want to collect are cells generated by Print[].
  • I do not want to have to recompute the cells that were outputted.
$\endgroup$
  • $\begingroup$ Sow/Reap? Or are you trying to avoid recomputing the printed values? $\endgroup$ – 2012rcampion Apr 14 '15 at 22:05
  • $\begingroup$ Are the results you want to collect are generated by Print? $\endgroup$ – kglr Apr 14 '15 at 22:13
  • $\begingroup$ @kuba just saw these, sorry! @2012rcampion yes, would be very costly to recompute them, @kguler yes they are generated by Print $\endgroup$ – M.R. Apr 15 '15 at 15:11
  • $\begingroup$ @M.R. Please edit your question to reflect that and I shall reopen this. $\endgroup$ – Mr.Wizard Apr 15 '15 at 15:31
  • $\begingroup$ @M.R. Thanks for the edit. Reopen vote casted. $\endgroup$ – Kuba Apr 15 '15 at 19:58
2
$\begingroup$

Let's say those cells are selected and the parent notebook is nb, you can do something like:

RawBoxes /@ NotebookRead[SelectedCells@nb][[;; , 1, 1]]
| improve this answer | |
$\endgroup$
2
$\begingroup$

Perhaps something like:

Lastt@Reap[For[j = 1, j <= 10, j++,Print[Sow@j]]]
| improve this answer | |
$\endgroup$
2
$\begingroup$

Just for something different, and given you seem to want the values of the print cells,

tmp = NotebookGet[EvaluationNotebook[]];
Cases[tmp, Cell[BoxData[p_], "Print", ___] :> p, \[Infinity]]

If you have other print cells that you do not want in the list, i.e. non integers, then you can obviously add some tests.

| improve this answer | |
$\endgroup$
1
$\begingroup$

If the results you wish to collect are all generated by Print, then you can use @Kuba's line in combination with NotebookFind:

NotebookFind[EvaluationNotebook[], "Print", All, CellStyle]; 
printresults = RawBoxes /@ NotebookRead[SelectedCells[]][[;; , 1, 1]];
| improve this answer | |
$\endgroup$

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