[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Initializing an NxN array

Ruby Student

3/19/2009 4:06:00 PM

[Note: parts of this message were removed to make it a legal post.]

Team,

What is the easiest way to initialize a square matrix?
For example, to initialize a 3x3 array elements to 0, I am doing what you
see below. But I am not sure how to proceed if, for instance, I want a NxN
array where N > 10 or a huge value?
I played a bit on IRB but could not find the way to do it easily.

irb(main):008:0> ary = Array.new(9,[0,0,0])
=> [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0,
0], [0, 0, 0], [0, 0, 0]]

Thank you

--
Ruby Student

6 Answers

matt

3/19/2009 4:51:00 PM

0

Ruby Student <ruby.student@gmail.com> wrote:

> ary = Array.new(9,[0,0,0])

Tread carefully, Grasshopper:

ary = Array.new(9,[0,0,0])
ary[0][0] = "surprise!"
p ary

:)

m.
--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Adam Gardner

3/19/2009 5:29:00 PM

0

Ruby Student wrote:
> Team,
>
> What is the easiest way to initialize a square matrix?
> For example, to initialize a 3x3 array elements to 0, I am doing what
> you
> see below. But I am not sure how to proceed if, for instance, I want a
> NxN
> array where N > 10 or a huge value?
> I played a bit on IRB but could not find the way to do it easily.
>
> irb(main):008:0> ary = Array.new(9,[0,0,0])
> => [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],
> [0, 0,
> 0], [0, 0, 0], [0, 0, 0]]
>
> Thank you

Wouldn't the best solution be:

require 'Matrix'
a = Matrix.zero(3)

?

Assuming you actually want to use it for Matrix math and not as just a
2D array.

If you want a 2D array, I'd do ary = Array.new(3) {|row| Array.new(3)
{|col| 0}}
--
Posted via http://www.ruby-....

Marcelo

3/19/2009 5:30:00 PM

0

On Thu, Mar 19, 2009 at 10:05, Ruby Student <ruby.student@gmail.com> wrote:

> For example, to initialize a 3x3 array elements to 0, I am doing what you
> see below. But I am not sure how to proceed if, for instance, I want a NxN
> array where N > 10 or a huge value?

N=3
Array.new(N) { Array.new(N,0) }

-m.

Evan Farrar

3/19/2009 5:34:00 PM

0

Since * is defined on array, you can start with a 1x1 array and multiply
to the size you need.


n = 3;
ary = [[0]*n]*n

results in:
ary => [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
--
Posted via http://www.ruby-....

Evan Farrar

3/19/2009 5:36:00 PM

0

Evan Farrar wrote:
> Since * is defined on array, you can start with a 1x1 array and multiply
> to the size you need.
>
>
> n = 3;
> ary = [[0]*n]*n
>
> results in:
> ary => [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

whoops! that has the same problem mentioned above:
irb(main):026:0> ary[0][0] = :foo; ary
=> [[:foo, 0, 0], [:foo, 0, 0], [:foo, 0, 0]]

I retract!
--
Posted via http://www.ruby-....

Ruby Student

3/19/2009 6:28:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Mar 19, 2009 at 1:36 PM, Evan Farrar <evanfarrar@gmail.com> wrote:

> Evan Farrar wrote:
> > Since * is defined on array, you can start with a 1x1 array and multiply
> > to the size you need.
> >
> >
> > n = 3;
> > ary = [[0]*n]*n
> >
> > results in:
> > ary => [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>
> whoops! that has the same problem mentioned above:
> irb(main):026:0> ary[0][0] = :foo; ary
> => [[:foo, 0, 0], [:foo, 0, 0], [:foo, 0, 0]]
>
> I retract!
> --
> Posted via http://www.ruby-....
>
>

Thanks to all, your help is appreicated!

--
Ruby Student