Yeah, this is definitely not how Julia works. Why not write a function that does what you want, then call it on x_foo and then later on x_bar?
-- John On Sep 7, 2014, at 4:34 PM, Alex <hollina...@gmail.com> wrote: > Hi John, > > I don't actually want to generate variables like that, but perform actions on > list of variables and I thought that example was a good way of boiling down > the problem I was having. So what I'm really trying to do is something more > like this. I guess I thought that ["foo", "bar"] was behaving like a macro > list of things I want this action to be done on, which is apparently not the > right way of thinking. > > n=5 > x_foo=zeros(n,n) > x_bar=zeros(n,n) > > for i in ["foo","bar"] > > x_$i = x_$i + n > println("x_$i") > > end > > x_foo > > > On Sunday, September 7, 2014 3:07:10 PM UTC-7, John Myles White wrote: > 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 <holli...@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 >