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 endI 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)]) endAs 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)]) endSomething like that...??
-
No.. in a similar way you that are using the
@ptsarray.@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.

Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement