Hi Alex,

You can’t use things like x_$i as variable names. The $i interpolation trick 
applies only to strings — no other construct in the entire language will 
perform this kind of interpolation.

You could use a macro to generate variables like this, but it’s not clear to me 
why you’d want to. Why are you creating variables in a loop?

Perhaps you’d prefer using a Dict instead?

n = 5
vars = Dict()
for name in [“foo”, “bar”]
        vars[“x_$(name)”] = zeros(n, n)
end
vars[“x_bar”]

 — John

On Sep 7, 2014, at 3:03 PM, Alex <hollina...@gmail.com> wrote:

> Hi Everyone, 
> 
> I've been having some trouble using a for loop to perform tasks over 
> similarly named vectors. This simple example below illustrates my problem 
> pretty well. I would like to create two 5x5 arrays of zeros, but with dynamic 
> names. In this case x_foo and x_bar would be the names. When I run this code, 
> it clearly is looping over the string set, but it is not actually creating 
> the array. I have had similar problem trying to call arrays into functions 
> dynamically based on their names. Clearly I am just incorrectly referencing 
> i, but I cannot figure it where the error is. I am trying to port a 
> complicated version of this code from Stata, where this is quite easy to do 
> by referencing i in your code as `i'. Sorry for such a novice question, but 
> this has been driving me nuts all day!
> 
> Thanks in advance!
> 
> Alex
> 
> n=5
> for i in ["foo","bar"]
>       x_$i=zeros(n,n)
>       println("x_$i")
> end
> x_bar

Reply via email to