Creating new variables in for-loop?
-
Hi!
Is it possible to create new variables in a for-loop?
Now I have something like this, which works:
loop_length = 3 for i in 0..(loop_length-1) if (i == 0) @yv_group0 = Sketchup.active_model.entities.add_group @yv_face0 = @yv_group0.entities.add_face (@pts[1], @pts[2], @pts[3], @pts[4]) end if (i == 1) @yv_group1 = Sketchup.active_model.entities.add_group @yv_face1 = @yv_group1.entities.add_face (@pts[5], @pts[6], @pts[7], @pts[8]) end if (i == 2) @yv_group2 = Sketchup.active_model.entities.add_group @yv_face2 = @yv_group2.entities.add_face (@pts[9], @pts[10], @pts[11], @pts[12]) end end
I think it would be smarter if it was possible to create new variabels in the for-loop, like:
for i in 0..(loop_length-1) @yv_group_i = Sketchup.active_model.entities.add_group @yv_face_i = @yv_group_i.entities.add_face (@pts[1+(i*4)], @pts[2+(i*4)], @pts[3+(i*4)], @pts[4+(i*4)]) end
As you can see, I would like to try to put the i on the variables.
Is that possible in some way??-Rasmus
-
Use an Array?
-
yes, I thought about but Im unsure how to do it.
face_array = Array.new(loop_length,Hash.new) for i in 0..(loop_length-1) face_array[i][@yv_group_i] = Sketchup.active_model.entities.add_group face_array[i][@yv_face_i] = @yv_group_i.entities.add_face (@pts[1+(i*4)], @pts[2+(i*4)], @pts[3+(i*4)], @pts[4+(i*4)]) end
Something like that...??
-
No.. in a similar way you that are using the
@pts
array.@yv_group = [] @yv_face = [] for i in 0..(loop_length-1) @yv_group[i] = Sketchup.active_model.entities.add_group() @yv_face[i] = @yv_group[i].entities.add_face (@pts[1+(i*4)], @pts[2+(i*4)], @pts[3+(i*4)], @pts[4+(i*4)]) end
-
Ok thank you Dan. I will try that.
Advertisement