[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Nooby question : multidimensional arrays.

marinho.tobolla@syncity.de

8/16/2006 9:37:00 AM

Well am I right, that in Ruby there are only one dimensional arrays, and that i have to add an array into an array to get multidimensional arrays, or is there a simpler more ruby like way to create them ?



6 Answers

Chris Gehlker

8/16/2006 10:50:00 AM

0


On Aug 16, 2006, at 2:37 AM, marinho.tobolla@syncity.de wrote:

> Well am I right, that in Ruby there are only one dimensional
> arrays, and that i have to add an array into an array to get
> multidimensional arrays, or is there a simpler more ruby like way
> to create them ?

Yep. You create them by just making arrays of arrays and you
reference them just like C arrays
irb(main):001:0> ary = []
=> []
irb(main):002:0> ary << [1, 2, 3] << %w{dog cat bird} << [8, 10, 17]
=> [[1, 2, 3], ["dog", "cat", "bird"], [8, 10, 17]]
irb(main):003:0> ary[1][2]
=> "bird"


--
The folly of mistaking a paradox for a discovery, a metaphor for a
proof, a torrent of verbiage for a spring of capital truths, and
oneself for an oracle, is inborn in us.
-Paul Valery, poet and philosopher (1871-1945)



Daniel Martin

8/16/2006 12:10:00 PM

0

"marinho.tobolla@syncity.de" <marinho.tobolla@syncity.de> writes:

> Well am I right, that in Ruby there are only one dimensional arrays,
> and that i have to add an array into an array to get
> multidimensional arrays, or is there a simpler more ruby like way to
> create them ?

Well, there's also the block that Array.new takes to give its initial
value:

a = Array.new(8) {Array.new(8) {0}}

This creates an 8 by 8 two-dimensional array initialized with all 0.

(Technically, you don't need that second block and could do the inner
bit as Array.new(8,0))

Craig Kim

8/16/2006 12:10:00 PM

0

One would have to think in Ruby. For a C programmer, it can be a hard
transition but once one gets used to the Ruby way, you will find the C way
bit inflexible and rigid. For example, in C, you might write:

#define NUM_COLS 20
#define NUM_ROWS 10
int ary[NUM_ROWS][NUM_COLS];
...
int i, j;
for (i = 0; i < NUM_ROWS; i++) {
for (j = 0; i < NUM_COLS; j++) {
ary[i][j] = some_code....
}
}

In Ruby, you don't need to worry about setting up iterator variables since
arrays know how to iterate themselves, e.g.

ary.each {|r|
r.each {|c|
...
}
}

If you need to access a specific element, you can still use the C-like
notation, i.e. ary[r][c].


----- Original Message -----
From: "Chris Gehlker" <canyonrat@mac.com>


>
> On Aug 16, 2006, at 2:37 AM, marinho.tobolla@syncity.de wrote:
>
>> Well am I right, that in Ruby there are only one dimensional arrays, and
>> that i have to add an array into an array to get multidimensional
>> arrays, or is there a simpler more ruby like way to create them ?
>
> Yep. You create them by just making arrays of arrays and you reference
> them just like C arrays
> irb(main):001:0> ary = []
> => []
> irb(main):002:0> ary << [1, 2, 3] << %w{dog cat bird} << [8, 10, 17]
> => [[1, 2, 3], ["dog", "cat", "bird"], [8, 10, 17]]
> irb(main):003:0> ary[1][2]
> => "bird"
>
>
> --
> The folly of mistaking a paradox for a discovery, a metaphor for a proof,
> a torrent of verbiage for a spring of capital truths, and oneself for an
> oracle, is inborn in us.
> -Paul Valery, poet and philosopher (1871-1945)
>
>
>
>
>


Jeff Schwab

8/16/2006 12:48:00 PM

0

marinho.tobolla@syncity.de wrote:
> Well am I right, that in Ruby there are only one dimensional arrays, and that i have to add an array into an array to get multidimensional arrays, or is there a simpler more ruby like way to create them ?

That's technically true, but it doesn't usually matter. In C, you're
declaring how much space you'll need to store data, but you don't have
to do that in Ruby. Ruby arrays will grow as necessary. If you just
want a 2d array, the simplest way is to create one and add to it as
necessary.

In the trivial case, you can do something like:

a = [[]]
a[0] << "hello" # Add a column to the first row.
a << ["world"] # Add a row.

Or you can initialize the size with:

a = [[0] * 6] * 4 # warning: sizes are (col,row), indices are (row,col)

The biggest problem is that you get a bunch of references to the same
object, so if you modify it, you mess up a whole column. For example:

a[3][5] = 15

Produces:

[[0, 0, 0, 0, 0, 15]
[0, 0, 0, 0, 0, 15]
[0, 0, 0, 0, 0, 15]
[0, 0, 0, 0, 0, 15]]

If you start using more dimensions, the row/column metaphor can break
down, so indexing can become a little confusing; you pretty much have to
start thinking of indices as (z,y,x) in some space.

Here's my philosophical take on this: Multi-dimensional rows generally
represent tables of data. Storing data in tables is a nice way to make
it accessible from procedural code, but is dead-set against the
object-oriented paradigm. If you're programming in Ruby, and you find
yourself with a multi-dimensional array, what you really want is a
single-dimensional array of objects, each of which represents a record.
If you're sure you want a multi-dimensional array, then you probably
are doing some kind of numeric computation, and frankly, you might be
happier implementing it in C or C++.

James Gray

8/16/2006 1:08:00 PM

0

On Aug 16, 2006, at 4:37 AM, marinho.tobolla@syncity.de wrote:

> Well am I right, that in Ruby there are only one dimensional
> arrays, and that i have to add an array into an array to get
> multidimensional arrays, or is there a simpler more ruby like way
> to create them ?

Others have given you great answers, but it's also worth pointing out
that you can make a multidimensional Array, if you like:

>> class Array2D
>> def initialize(width, height)
>> @data = Array.new(width) { Array.new(height) }
>> end
>> def [](x, y)
>> @data[x][y]
>> end
>> def []=(x, y, value)
>> @data[x][y] = value
>> end
>> end
=> nil
>> arr = Array2D.new(3, 2)
=> #<Array2D:0x31d0b0 @data=[[nil, nil], [nil, nil], [nil, nil]]>
>> arr[1, 1] = "Hello"
=> "Hello"
>> arr
=> #<Array2D:0x31d0b0 @data=[[nil, nil], [nil, "Hello"], [nil, nil]]>
>> arr[1, 1]
=> "Hello"

James Edward Gray II


David Vallner

8/17/2006 2:28:00 PM

0

On Wed, 2006-08-16 at 18:37 +0900, marinho.tobolla@syncity.de wrote:
> Well am I right, that in Ruby there are only one dimensional arrays, and that i have to add an array into an array to get multidimensional arrays, or is there a simpler more ruby like way to create them ?

Or, if you need a speed burst, NArray [http://narray.ruby...]
could help. Never used it myself yet though.

David Vallner