Question about loop?

Dear all,

I want to generate x1,x2,…x10, How can I write the code?

julia> for i in 1:10
       x$i = i
       end

I want to generate x1 = 1, x2 = 2...

for i in 1:10
   eval( Meta.parse("x$i = $i") )
end

@oheil, I am not sure if this kind of question should be answered at face value. This will always create these variables as global variables and it is terribly slow. Probably no novice should be introduced to eval and Meta.parse this carelessly.

5 Likes

Here I want to plot 10 graphs.

 for i in 1:10
           data = @where(df, :Firm .== i)
           p$i = @df data plot(:Year, :Inv)
       end

I want to know how to get p1, p2, p3 …p10.

I would make a list or a dictionary

ps = []
for i in 1:10
    data = @where(df, :Firm .== i)
    push!(ps, (@df data plot(:Year, :Inv))
end

then you can use

ps[1]

to access the plots

2 Likes

Thanks. This is a good idea.

It’s somehow becoming complicated here.