For Loop over set of vector names

1,067 views
Skip to first unread message

Alex

unread,
Sep 7, 2014, 6:03:01 PM9/7/14
to julia...@googlegroups.com
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

Alex

unread,
Sep 7, 2014, 6:04:47 PM9/7/14
to julia...@googlegroups.com

EDIT: Sorry I forgot to add the output. 

julia> n=5

5

julia> for i in ["foo","bar"]

       x_$i=zeros(n,n)

       println("x_$i")

       end

x_foo

x_bar

julia> x_foo

ERROR: x_foo not defined

John Myles White

unread,
Sep 7, 2014, 6:07:10 PM9/7/14
to julia...@googlegroups.com
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

Alex

unread,
Sep 7, 2014, 7:34:45 PM9/7/14
to julia...@googlegroups.com
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


John Myles White

unread,
Sep 7, 2014, 7:36:00 PM9/7/14
to julia...@googlegroups.com
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

gael....@gmail.com

unread,
Sep 8, 2014, 4:51:11 AM9/8/14
to julia...@googlegroups.com
Hi Alex,

As John said, a dict would be far better.

If you feel the need to access the bounding names through a template like this, then you'd better build a dict from the start or just before using your trick.

If your elements are well known, you could even directly use an array : if you need both the content and the name, you can 'zip' them or 'enumerate' them.

On the fly bounding name generation (even just reading its content) is considered a bad practice because these names should only be relevant to the programmer, not to the program : you are really trying to achieve a functional equivalent to a dict.

gael....@gmail.com

unread,
Sep 8, 2014, 4:58:14 AM9/8/14
to julia...@googlegroups.com
By the way, if your "actions" on these variables are independant and in-place, the array method would have the extra advantage that it can be made parallel at no cost and that it whould let you access the updated values through the array but also through their original names.

Alex

unread,
Sep 9, 2014, 3:36:31 PM9/9/14
to julia...@googlegroups.com
Thanks for the tips! As an academic trying to move lots of code over from other languages my lack of knowledge in coding fundamentals is being exposed a bit.

Alex


On Sunday, September 7, 2014 3:03:01 PM UTC-7, Alex wrote:
Reply all
Reply to author
Forward
0 new messages