From: daz Date: 2003-06-22T12:26:26+09:00 Subject: Re: I worked it out (was Re: 2 dimentional array in ruby) "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< 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