12

How to use Row[Table[{B[i],0,i},{i,0,2}],","] directly in Do command? I mean that the following command

Row[Table[{B[i],0,i},{i,0,2}],","]

gives

{B[0], 0, 0},{B[1], 0, 1},{B[2],0,2}

But, the following command returns the error

"Do::nliter: Non-list iterator Row[Table[{B[i], 0, i}, {i, 0, 2}], ,] at position 2 does not evaluate to a real numeric value."

Do[Print[B[0]+B[1]+B[2]],Row[Table[{B[i],0,i},{i,0,2}],","]]

Of course, one can type instead by hand the following:

Do[Print[B[0]+B[1]+B[2]],{B[0],0,0},{B[1],0,1},{B[2],0,2}] 

But, I feel that typing an output by hand again is not really an optimal method.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
veo
  • 255
  • 1
  • 5

3 Answers3

15

What you actually want is to create a Sequence from the Table to be used as your iterators.

You can do this with

Do[Print[B[0] + B[1] + B[2]], Sequence @@ Table[{B[i], 0, i}, {i, 0, 2}] // 
Evaluate]

(*0
  1
  2
  1
  2
  3*)

Or, so you don't have to force evaluation,

Do[Print[B[0] + B[1] + B[2]], ##] & @@ Table[{B[i], 0, i}, {i, 0, 2}]
NonDairyNeutrino
  • 7,810
  • 1
  • 14
  • 29
7

Perhaps you can avoid Do and instead use Tuples:

Tuples @ Range[0, {0, 1, 2}]

{{0, 0, 0}, {0, 0, 1}, {0, 0, 2}, {0, 1, 0}, {0, 1, 1}, {0, 1, 2}}

You can then use Total to sum each tuple:

Total[
    Tuples @ Range[0, {0, 1, 2}],
    {2}
]

{0, 1, 2, 1, 2, 3}

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
5
Do[Print[B[0] + B[1] + B[2]], 
 Evaluate[Sequence @@ First@ Row[Table[{B[i], 0, i}, {i, 0, 2}], ","]]]

or

row = Row[Table[{B[i], 0, i}, {i, 0, 2}], ","];
Do[Print[B[0] + B[1] + B[2]], Evaluate[Sequence @@ row[[1]]]]
kglr
  • 394,356
  • 18
  • 477
  • 896