How can I do the following? I have code inside a Module such as Module[{a1,a2,a3,a4}, code...]
. In this code I want to refer to these constants efficiently (for a large number of them) so I would construct them in ways such as Table[ToExpression["a" <> ToString[i]], {i, 4}]
. The important point is that they are constructed as a1
(etc) after the module has done its renaming to a unique variable. So this does not work, see:
Module[{a1},
MemberQ[Table[ToExpression["a" <> ToString[i]], {i, 3}], a1]]
Is it possible to do this correctly? (By the way, this is almost the opposite question of Refering to global variables inside modules.)
As often, the time needed to formulating the question was enough to actually find a solution. While it isn't an answer to the question, the better way to avoid the whole problem is to assign multiple downvalues to one a
, i.e. a[1]
,a[2]
,...
Block
instead ofModule
? $\endgroup$