Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2001

Re: How can I generate new variables?

From 7stud -- <bbxx789_05ss@yahoo.com>
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 <bec536840ae5b1c75ae0d824c20af7d3@ruby-forum.com> (permalink)
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 <bec536840ae5b1c75ae0d824c20af7d3@ruby-forum.com>
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

Show key headers only | View raw


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/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How can I generate new variables? "Kyle X." <haebooty@yahoo.com> - 2011-03-30 19:42 -0500
  Re: How can I generate new variables? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-30 20:14 -0500
    Re: How can I generate new variables? "Kyle X." <haebooty@yahoo.com> - 2011-03-30 22:08 -0500
      Re: How can I generate new variables? Gunther Diemant <g.diemant@gmx.net> - 2011-03-31 03:08 -0500
        Re: How can I generate new variables? "Kyle X." <haebooty@yahoo.com> - 2011-03-31 14:30 -0500
      Re: How can I generate new variables? Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 03:11 -0500
      Re: How can I generate new variables? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-03-31 20:06 -0500
  Re: How can I generate new variables? "Kyle X." <haebooty@yahoo.com> - 2011-04-02 01:18 -0500
    Re: How can I generate new variables? Robert Klemme <shortcutter@googlemail.com> - 2011-04-04 02:15 -0500

csiph-web