# How to combine selected out cells into a list? [closed]

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

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.

## closed as off-topic by Kuba♦, Karsten 7., bbgodfrey, Bob Hanlon, m_goldbergApr 15 '15 at 14:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Kuba, Karsten 7., bbgodfrey, Bob Hanlon, m_goldberg
If this question can be reworded to fit the rules in the help center, please edit the question.

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

## 4 Answers

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

RawBoxes /@ NotebookRead[SelectedCells@nb][[;; , 1, 1]]


Perhaps something like:

Lastt@Reap[For[j = 1, j <= 10, j++,Print[Sow@j]]]


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.

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]];