[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

2 dimensional array

Shuaib Zahda

10/17/2007 10:53:00 AM

Dear all

I worked for quite some hours and i googled the two dimensional array in
ruby and yet my problem is partially solved

I want to declare 2-dimensional arrays it has 6 columns but uknow number
of columns.
i tried this way
array = [][] # did not work
array = [[],[]] # it works but only for two elements.

another question I believe the answer is no but i want to confirm
can we have multi-dimensional hash? :)

any one has any idea to share
Regards
Shuaib
--
Posted via http://www.ruby-....

6 Answers

Stefano Crocco

10/17/2007 11:51:00 AM

0

Alle mercoledì 17 ottobre 2007, Shuaib Zahda ha scritto:
> Dear all
>
> I worked for quite some hours and i googled the two dimensional array in
> ruby and yet my problem is partially solved
>
> I want to declare 2-dimensional arrays it has 6 columns but uknow number
> of columns.
> i tried this way
> array = [][] # did not work
> array = [[],[]] # it works but only for two elements.
>
> another question I believe the answer is no but i want to confirm
> can we have multi-dimensional hash? :)
>
> any one has any idea to share
> Regards
> Shuaib

Ruby doesn't have 2-dimensional arrays, but you can use nested arrays to
achieve the same effect. If you want an array with 6 columns and any number
of rows, you can do something like:

a = Array.new(6){[]}

This creates an array of 6 elements, each of one contains an array. Each
element represents a column. The entries of the column are stored into the
nested array.

You can store entries like this:

a[2][3] = 1

This sets the element 3 of the row 2 to be one.

Of course, this approach allows you to have columns with different sizes. For
example, since each clumn is an array, you can do this:

a[1] << 2

This increases the size of the column 1 by one, by appending an element to it.
If you want to avoid this, you can wrap the 2d array in a class:

class Array2D

def initialize cols, default = nil
@data = Array.new(cols){[]}
@default = default
end

def append_row
@data.each{|c| c << @default}
end

def [](col, row=nil)
if row then @data[col][row]
else @data[col].dup
end
end

def []=(col, row, value)
raise IndexError if col >= @data.size or row >= @data[0].size
@data[col][row]=value
end

def each_col
@data.each{|c| yield c.dup}
end

def each
@data.each do |c|
c.each{|i| yield i}
end
end

end

USAGE:

a = Array2D.new 3, :a
3.times{a.append_row}
a[0, 0] = :b
a[1, 2] = :c
p a[0]
p a[1]
p a[2]

Regarding your second question, what do you mean by "multidimensional hash"?

Stefano

Shuaib Zahda

10/17/2007 1:31:00 PM

0

Dear Stefano
Thanks for the reply. The way you gave me is basically makes six rows
and unlimited number of columns
I want the other way aroud
basically I have this table

field | type | key | default | extra | null
name | string | yes | null | auto_increment| null
id | integer | .... .... . .. . . . . ..
address

basically is a small database structure

any idea? thanks
--
Posted via http://www.ruby-....

John Joyce

10/17/2007 4:47:00 PM

0


On Oct 17, 2007, at 8:31 AM, Shuaib Zahda wrote:

> Dear Stefano
> Thanks for the reply. The way you gave me is basically makes six rows
> and unlimited number of columns
> I want the other way aroud
> basically I have this table
>
> field | type | key | default | extra | null
> name | string | yes | null | auto_increment| null
> id | integer | .... .... . .. . . . . ..
> address
>
> basically is a small database structure
>
> any idea? thanks
> --
> Posted via http://www.ruby-....
>

Shuaib, If you want a simple database, you might consider SQLite,
there is a ruby gem for it and you can use ORMs such as ActiveRecord
too.

Robert Klemme

10/17/2007 6:16:00 PM

0

On 17.10.2007 15:31, Shuaib Zahda wrote:
> Dear Stefano
> Thanks for the reply. The way you gave me is basically makes six rows
> and unlimited number of columns

Well, it depends on what dimension you view as row and column.

> I want the other way aroud
> basically I have this table
>
> field | type | key | default | extra | null
> name | string | yes | null | auto_increment| null
> id | integer | .... .... . .. . . . . ..
> address
>
> basically is a small database structure
>
> any idea? thanks

Why don't you just create a Struct with the six fields you have in mind
and stuff those instances in an Array?

And what is a "multi dimensional hash"?

Kind regards

robert

Shuaib Zahda

10/18/2007 2:11:00 AM

0

Dear all
Thanks for the help, I managed to do it using Mr. Michael
Bevilacqua-Linn way. and it works

just to share with u the lines of code that make the two - dimensional
array.
note that the number of columns can be extended.

columns = Array.new()
columns << Array.new
columns[i][0] = col[0]
columns[i][1] = col[1]

Regarding the multidimensional hash what i meant is hash of hashes.
Thanks
--
Posted via http://www.ruby-....

Todd Benson

10/19/2007 9:36:00 PM

0

On 10/19/07, Victor Reyes <victor.reyes@gmail.com> wrote:
> Sorry guys, I am very slow.
> In conclusion. how would the following be represented in Ruby?
> It is a 3x3 matrix.
>
> 11 76 -34 31 -66 71 -1 63 34
> Thank you

It depends on what you want to do. If you just want to represent it
as a nested array, you could...

my_matrix = [[11, 76, -34], [31, -66, 71], [-1 63 34]]

As you notice, this is still 2-dimensional.


For math type matrix functionality, there is the matrix standard library...

require 'matrix'

which includes methods that return a manipulated matrix (represented
by the Matrix object), like inverse, rank, determinant, algebraic
functions, etc. I believe you can also go back and forth between
nested arrays and Matrix objects using Matrix#to_a and
Matrix#[](*rows). Somebody else might be able to fill you in better.

You can find documentation for that under the standard library docs at
http://www.ru....

hth,
Todd