0
$\begingroup$

This question already has an answer here:

Let there be two lists,

exp = {{i, i}, {j, j}};
exp2 = {{r, r}, {o, o}};

and there are two Table commands to access them. Then,

Table[exp[[i, j]], {i, 1, Length[exp]}, {j, 1, Length[exp[[i]]]}]

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

Table[exp2[[i, j]], {i, 1, Length[exp2]}, {j, 1, Length[exp2[[i]]]}]

{{r, r}, {o, o}}

So, the variables are being replaced in first case. In case of other languages this problem won't occur but here they are getting iterator value.

If I want to avoid this allocation and take data from set exp what can be done. I tried Holds and Defer but didn't kind of expected result.

$\endgroup$

marked as duplicate by Leonid Shifrin, m_goldberg, Oleksandr R., Artes, Sjoerd C. de Vries Jul 14 '13 at 17:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Module[{i, j}, Table... or do not use i,j as iterators. $\endgroup$ – Kuba Jul 14 '13 at 12:47
  • $\begingroup$ Ya I can always use different names but just in case this situation occurs than how I shall prevent allocation...is it possible ? $\endgroup$ – Rorschach Jul 14 '13 at 12:58
  • $\begingroup$ Module[{i, j}, Table[exp[[i,j]],{i,2},{j,2}]] like this? $\endgroup$ – Kuba Jul 14 '13 at 12:59
  • 1
    $\begingroup$ If you really insist on using Hold, use it like ReleaseHold@Table[Hold@exp[[#1, #2]] &[i, j], {i, 1, 2}, {j, 1, 2}] or ReleaseHold@ Table[With[{i = i, j = j}, Hold@exp[[i, j]]], {i, 1, 2}, {j, 1, 2}] $\endgroup$ – panda-34 Jul 14 '13 at 13:36
  • 1
    $\begingroup$ Dupolicate of this and this. $\endgroup$ – Leonid Shifrin Jul 14 '13 at 14:15