From: Greg Barozzi Date: 2009-11-15T07:33:42+09:00 Subject: Re: Array within an array Mahadev Ittina wrote: > I am trying to create a array, which basically forms a grid, which is > evenly spaced in a Cartesian plane. The spacing is var1 and var2, in the > x and z directions. Plan is to copy columns on these 'points', which is > done in Sketchup using Ruby API scripts. So there are going to 8 and 9 > columns respectively, and there are points when n = i, where there will > be common columns. > > This is not working out for me very well. It makes sense to me, somehow > it doesn't seem to be working (it returns with no values).Any > suggestions? And If I want floors to this array, i.e. making it 3D, how > will it work? Hiya Mahadev if you want to define a 10x10 multi-dimensional-array you can use the map method mda = (0..9).map { (0..9).map { nil } } this builds a grid for you with nil as the default element in each cell of the grid. (You can use something other than nil if you want.) And then if you want to iterate through it there are a few ways you can do it but like MLK says above one way is to use the times method like this: mda.length.times do |x| mda[0].length.times do |y| mda[x][y] = some_new_value end end I like to chain array lengths through when I use times so that if I need to later expand or shorten my array length then I don't have to modify the code in too many places. And that should pretty much get you started. =) Good luck and happy rubying! -greg -- Posted via http://www.ruby-forum.com/.