0
$\begingroup$

I attempted to write part of my code after what I found at ( get while loop output into a list ) which is the post "get while loop into a list (duplicate)", which there shows an example

n = 1;
While[n < 10, 
Print[RandomSample[Join[Table["a", {2}], Table["b", {2}]], 2]]; n++]

This is not my code, but it gives an idea of why I structured my code this way. What you'll see below is a while loop in which I used reap and sow in hopes of making a list, which is all I need. I was successful in getting the info I needed but strangely it seems before the output starts I get a vertically lined version of the actual output that follows in the list form I specified. Here's the part of my code making it happen and what happens:

Flatten[Drop[Reap[d = 0; While[d < (2^n), Print[Sow[Extract[Drop[First[RealDigits[Mod[f (2^d), 1] + 1]], 1], (h - (d +4))]]]; d++]], 1]]

Which gives me (for my not shown variable values):

4

2

6

5

9

7

1

0

Out[(some number)]= {4, 2, 6, 5, 9, 7, 1, 0}

I've tried everything short of throwing the kitchen sink at it to just show me the list-form output I designed but it keeps preceding the output with that stupid column and as you can imagine, as I try to evaluate large data it keeps extending to hundreds of pages. Please help me shut this thing up. The desired output should just be the list at the end, {4, 2, 6, 5, 9, 7, 1, 0}. The variable values shouldn't matter since the form is the problem.

$\endgroup$
8
  • 2
    $\begingroup$ Have a look at Table, Map, Fold, Nest and friends; if your are using loops in Mathematica you are doing something wrong. Check out Alternatives to procedural loops and iterating over lists in Mathematica $\endgroup$
    – Sascha
    Commented Nov 23, 2016 at 12:03
  • $\begingroup$ It would be helpful if you just gave (a sample of) your data and what you wanted to do with it, without f and h we can't even run your Franken code. $\endgroup$
    – Feyre
    Commented Nov 23, 2016 at 12:05
  • 4
    $\begingroup$ The column of numbers before the output is due to Print $\endgroup$
    – Sascha
    Commented Nov 23, 2016 at 12:05
  • 1
    $\begingroup$ @Sascha From what I'm seeing, he's Print[]ing his expression, and then sowing the output. It's rather convoluted. $\endgroup$
    – Feyre
    Commented Nov 23, 2016 at 12:06
  • 1
    $\begingroup$ Print[] literally just prints the expression to output. But I don't see why you are using Sow and Reap in the first place. What's wrong with just a Table[]? $\endgroup$
    – Feyre
    Commented Nov 23, 2016 at 12:32

1 Answer 1

1
$\begingroup$

Just drop the call to Print.

With[{n = 3, f = N[Pi], h = 0},
  Flatten[
    Drop[
      Reap[
        d = 0;
        While[d < (2^n),
          Sow[
            Extract[
              Drop[First[RealDigits[Mod[f (2^d), 1] + 1]], 1],
              (h - (d + 4))]]; 
          d++]], 
      1]]]

{9, 7, 3, 8, 5, 9, 9, 5}

$\endgroup$
2
  • $\begingroup$ Damn that's some beautiful code, thank you! $\endgroup$ Commented Nov 23, 2016 at 21:03
  • 1
    $\begingroup$ @Arlen McCracken That code is very far from beautiful (infact code like this makes most people's toe nails curl). See the comment I left on your original question about alternatives to procedural loops and have a look around popular questions to see how people code in Mathematica elegantly. $\endgroup$
    – Sascha
    Commented Nov 24, 2016 at 11:00

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