# Array within an array

**URL:** <https://www.ruby-forum.com/t/array-within-an-array/178981>\
**Category:** Ruby\
**Created:** [November 14, 2009, 6:03pm UTC](https://www.ruby-forum.com/t/array-within-an-array/178981 "2009-11-14T18:03:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 14, 2009, 6:03pm UTC](https://www.ruby-forum.com/t/array-within-an-array/178981/1 "2009-11-14T18:03:12Z")

</div>

$array\_floor = []  
for i in (0…7)  
for n in (0…8)  
$array\_floor [n,i] = [i\*var1,0.0,n\*var2]  
end  
end

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?

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 14, 2009, 8:56pm UTC](https://www.ruby-forum.com/t/array-within-an-array/178981/2 "2009-11-14T20:56:14Z")

</div>

Mahadev I. wrote:

> $array\_floor = []  
> for i in (0…7)  
> for n in (0…8)  
> $array\_floor [n,i] = [i\*var1,0.0,n\*var2]  
> end  
> end

Almost every line of this code is problematic. To fix:

- Never use global variables.
- Instead of your “for” constructs, use “times”.
- Use the proper syntax for multidimensional arrays (which is  
array[x][y], not array[x, y]). array[x, y] has a completely different  
meaning.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 14, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/array-within-an-array/178981/3 "2009-11-14T23:33:39Z")

</div>

Mahadev I. 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

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 15, 2009, 12:05am UTC](https://www.ruby-forum.com/t/array-within-an-array/178981/4 "2009-11-15T00:05:52Z")

</div>

On 14.11.2009 18:03, Mahadev I. wrote:

> 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?

$array\_floor[n,i]=… does not assign to a two dimensional Array.  
Rather, it assigns to a portion of the array:

irb(main):004:0\> a=(1…10).to\_a  
=\> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
irb(main):005:0\> a[3,2] = 555  
=\> 555  
irb(main):006:0\> a  
=\> [1, 2, 3, 555, 6, 7, 8, 9, 10]  
irb(main):007:0\>

Here’s another option

array\_floor = Array.new 7 do |i|  
Array.new 8 do |n|  
[i,0,n]  
end  
end

Note though that this will create a three dimensional matrix. Btw, for  
structures like this “pp” is a useful utility for printing:

require ‘pp’  
pp array\_floor

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mittina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mittina](https://www.ruby-forum.com/u/mittina)\
**Post date:** [November 18, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/array-within-an-array/178981/5 "2009-11-18T21:46:04Z")

</div>

that is interesting… i am only discovering more and more how  
inefficient my method is… thank you for your answers… I will post if  
there are any updates

Regards,  
Mahadev
