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 2011-03-30 20:14 -0500
Organization Service de news de lacave.net
Message-ID <bec536840ae5b1c75ae0d824c20af7d3@ruby-forum.com> (permalink)
References <33ba41ea1d06d357f353d2462a951026@ruby-forum.com>

Show all headers | 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