# 2 dimentional array in ruby - niewbie

**URL:** <https://rubytalk.org/t/2-dimentional-array-in-ruby-niewbie/6497>\
**Category:** ruby-talk\
**Created:** [21 June 2003 23:24 UTC](https://rubytalk.org/t/2-dimentional-array-in-ruby-niewbie/6497 "2003-06-21T23:24:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacek\_Podkanski2](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@Jacek\_Podkanski2](https://rubytalk.org/u/Jacek_Podkanski2)\
**Post date:** [21 June 2003 23:24 UTC](https://rubytalk.org/t/2-dimentional-array-in-ruby-niewbie/6497/1 "2003-06-21T23:24:51Z")

</div>

Please help  
I want to create a multiplication table in ruby.  
I want a 2 dimentional array  
and the do something like that

2darray.new  
for row in 1…10  
for col in 1…10  
2darray[row,col]=row\*col  
end  
end

How do I do it in Ruby

> **···**
>
> –  
> Jacek Podkanski

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [21 June 2003 23:45 UTC](https://rubytalk.org/t/2-dimentional-array-in-ruby-niewbie/6497/2 "2003-06-21T23:45:04Z")

</div>

> How do I do it in Ruby

How about this:

> ruby xxyymm.rb  
> rows-first: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12], [0, 4, 8, 12, 16], [0, 5, 10, 15, 20], [0, 6, 12, 18, 24], [0, 7, 14, 21, 28]]  
> cols-first: [[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7], [0, 2, 4, 6, 8, 10, 12, 14], [0, 3, 6, 9, 12, 15, 18, 21], [0, 4, 8, 12, 16, 20, 24, 28]]  
> expand -t2 xxyymm.rb  
> x=5  
> y=8

data = Array.new(y) { Array.new(x) }  
for j in (0…y-1)  
for i in (0…x-1)  
data[j][i] = i \* j  
end  
end

puts “rows-first: #{data.inspect}”  
puts “cols-first: #{data.transpose.inspect}”

> **···**
>
> On Sun, 22 Jun 2003 01:14:38 +0100, Jacek Podkanski wrote:
> 
> > 
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [21 June 2003 23:46 UTC](https://rubytalk.org/t/2-dimentional-array-in-ruby-niewbie/6497/3 "2003-06-21T23:46:37Z")

</div>

There’s some information here.

[http://www.rubygarden.org/ruby?MultiDimensionalArrays](http://www.rubygarden.org/ruby?MultiDimensionalArrays)

Unfortunately, this page is not returned when one searches the Wiki  
for “multi array”.

Gavin

> **···**
>
> On Sunday, June 22, 2003, 9:24:51 AM, Jacek wrote:
> 
> > I want a 2 dimentional array

---

<div class="post-metadata">

**Author:** ![Jacek\_Podkanski2](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@Jacek\_Podkanski2](https://rubytalk.org/u/Jacek_Podkanski2)\
**Post date:** [22 June 2003 00:45 UTC](https://rubytalk.org/t/2-dimentional-array-in-ruby-niewbie/6497/4 "2003-06-22T00:45:38Z")

</div>

Jacek Podkanski wrote:

> Please help  
> I want to create a multiplication table in ruby.  
> I want a 2 dimentional array  
> and the do something like that
> 
> 2darray.new  
> for row in 1..10  
> for col in 1..10  
> 2darray[row,col]=row\*col  
> end  
> end
> 
> How do I do it in Ruby

I think I worked out how to do it, but still I don’t understand why the 2  
outouts are different, and only second is correct.

#! /usr/bin/ruby

#table acces function  
def ta(table,row,column,value)  
r=table[row]; r[column]=value  
end

#### create 2 dimensional array

t=Array.new  
for x in 0..9  
row=Array.new  
for y in 0..9  
row[y]=x+1\*y+1  
end#for  
t\<\<row  
end#for

> **···**
>
> #######################################  
> puts t.inspect
> 
> #create multiplication table  
> for x in 0..9  
> for y in 0..9  
> ta(t,x,y,x+1\*y+1)  
> end  
> end
> 
> puts t.inspect
> 
> –  
> Jacek Podkanski

---

<div class="post-metadata">

**Author:** ![Jacek\_Podkanski2](https://avatars.discourse-cdn.com/v4/letter/j/ea666f/32.png) [@Jacek\_Podkanski2](https://rubytalk.org/u/Jacek_Podkanski2)\
**Post date:** [22 June 2003 01:05 UTC](https://rubytalk.org/t/2-dimentional-array-in-ruby-niewbie/6497/5 "2003-06-22T01:05:50Z")

</div>

This is my way of doing it. It’s different from other’s suggestions, but it  
suits my needs. I anyody comes up with shorter solution please let me know.

#! /usr/bin/ruby

#table acces function  
def ta(table,row,column,value)  
r=table[row]; r[column]=value  
end

#### create 2 dimensional array

t=Array.new  
for x in 0…9  
row=Array.new  
for y in 0…9  
row[y]=(x+1)\*(y+1)  
end#for  
t\<\<row  
end#for

> **···**
>
> #######################################  
> puts t.inspect
> 
> #create multiplication table  
> for x in 0…9  
> for y in 0…9  
> ta(t,x,y,(x+1)\*(y+1) )  
> end  
> end
> 
> puts t.inspect
> 
> –  
> Jacek Podkanski

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [23 June 2003 08:34 UTC](https://rubytalk.org/t/2-dimentional-array-in-ruby-niewbie/6497/6 "2003-06-23T08:34:33Z")

</div>

How about an OO solution:

class MultTable  
attr\_reader :dim\_x, :dim\_y

def initialize(dim\_x, dim\_y)  
@dim\_x, @dim\_y = dim\_x, dim\_y  
end

def ; x\*y; end

def row(x)  
a=  
dim\_y.times{|y| a \<\< self[x,y]}  
a  
end

def col(y)  
a=  
dim\_x.times{|x| a \<\< self[x,y]}  
a  
end  
end

mt = MultTable.new(10, 10)

mt.dim\_x.times do |x|  
p mt.row(x)  
end

Cheers

```
robert

```

“Jacek Podkanski”  
\<[jacekpodkanski.at.supanet.dot.com@somewhere.where.you.should.npt.spam.com](mailto:jacekpodkanski.at.supanet.dot.com@somewhere.where.you.should.npt.spam.com)

> schrieb im Newsbeitrag  
> [news:NF5Ja.3024$425.126690@newsfep2-gui.server.ntli.net](mailto:news:NF5Ja.3024$425.126690@newsfep2-gui.server.ntli.net)…

> **···**
>
> > Please help  
> > I want to create a multiplication table in ruby.  
> > I want a 2 dimentional array  
> > and the do something like that
> > 
> > 2darray.new  
> > for row in 1…10  
> > for col in 1…10  
> > 2darray[row,col]=row\*col  
> > end  
> > end
> > 
> > ## How do I do it in Ruby
> > 
> > Jacek Podkanski

---

<div class="post-metadata">

**Author:** ![daz](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daz](https://rubytalk.org/u/daz)\
**Post date:** [22 June 2003 03:26 UTC](https://rubytalk.org/t/2-dimentional-array-in-ruby-niewbie/6497/7 "2003-06-22T03:26:26Z")

</div>

“Jacek Podkanski” wrote:

> This is my way of doing it. It’s different from other’s suggestions, but it  
> suits my needs. I anyody comes up with shorter solution please let me know.
> 
> #! /usr/bin/ruby
> 
> #table acces function  
> def ta(table,row,column,value)  
> r=table[row]; r[column]=value  
> end
> 
> #### create 2 dimensional array
> 
> t=Array.new  
> for x in 0..9  
> row=Array.new  
> for y in 0..9  
> row[y]=(x+1)\*(y+1)  
> end#for  
> t\<\<row  
> end#for  
> #######################################  
> puts t.inspect
> 
> –  
> Jacek Podkanski

Hi Jacek,

map (same as collect) returns an array, so you can  
create an array of 10 arrays, each having 10 elements  
like this …

### Create multiplication table

Mult = (1..10).map{|x| (1..10).map{|y| x \* y }}

### Table access function

def ta(row, column)  
Mult[row-1][column-1]  
end

### Test it

p ta(1, 1) #=\> 1  
p ta(7, 6) #=\> 42  
p ta(6, 7) #=\> 42  
p ta(10, 10) #=\> 100

### Display table

Mult.each{|r| r.each{|c| printf(“%4d”, c)}; puts}

daz
