From: 7stud -- Newsgroups: comp.lang.ruby Subject: Re: How can I generate new variables? Date: Wed, 30 Mar 2011 20:14:49 -0500 Organization: Service de news de lacave.net Lines: 56 Message-ID: References: <33ba41ea1d06d357f353d2462a951026@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1301534108 56770 65.111.164.187 (31 Mar 2011 01:15:08 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Thu, 31 Mar 2011 01:15:08 +0000 (UTC) In-Reply-To: <33ba41ea1d06d357f353d2462a951026@ruby-forum.com> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 380647 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!talisker.lacave.net!lacave.net!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2001 Kyle X. wrote in post #990090: > Hello, this is not so much a sketchup question as a Ruby one that I > cannot figure out. What I am trying to do is create new variables that > all have the same base name then add a number to make a series of > variables named like wall1, wall2, wall3, ect. > That is a typical beginner question. You NEVER need to do that. All computer languages have arrays for that purpose. > What I am trying to do is take an array, lets call it originalarray, > which has say 64 objects in it, and create 8 smaller arrays from this. > So array1 would be orginalarray[0-7], array2 would be > originalarray[8-15], and so on for 8 new arrays. The problem is I do not > know the length of the original array, as it is being populated by data > from an xml sheet, but I know the multiple of how it is being created, > ie by multiples of 8. > > I have been trying to do this using loops doing something like: > > n=0 > while n != originalarray.length/8 > n=n.to_s > array+n=originalarray[0..7] > n=n.to_i > n=n+1 > end > > > > I realize that is doesn't work at all in that you cannot make variables > like this, I am just trying to give a better picture of what my goal is. > So I am wondering is it possible to accomplish this at all, and if so > how? Using arrays. An array can contain anything--including other arrays: require 'enumerator' #not necessary in ruby 1.9 master_arr = [] data = [1, 2, 3, 4, 5, 6, 7, 8] data.each_slice(3) do |sub_arr| master_arr << sub_arr end p master_arr --output:-- [[1, 2, 3], [4, 5, 6], [7, 8]] -- Posted via http://www.ruby-forum.com/.