How to use string substitution in variable names

Don’t use metaprogramming or generated variable names. Use a data structure. For example, an array e of the 7 unit vectors in ℝ⁷ can be constructed with:

e = [ let v = zeros(7); v[i] = 1; v; end for i in 1:7 ]

This way, you can refer to e[i] programmatically rather than (as you seem to want) having 7 separate variables e_1, e_2, …, e_7. For example:

julia> e[3]
7-element Array{Float64,1}:
 0.0
 0.0
 1.0
 0.0
 0.0
 0.0
 0.0

See also this thread: How to warn new users away from metaprogramming

10 Likes