[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Strange behavior of Array

seebs

6/10/2007 3:18:00 AM

In message <ae8a916c0706092013w592e1730y59b0c9d7d55d7005@mail.gmail.com>, "huang zhimin" writes
:
>I want a multidimensional array with initial value 0, so I create it by

>arr = Array.new(3, Array.new(3, 0)) => [[0, 0, 0], [0, 0, 0], [0, 0,
>0]]

You have now created an array consisting of three copies of the SAME
array [0,0,0].

This is a FAQ.

-s

1 Answer

Dan Zwell

6/10/2007 3:28:00 AM

0

Peter Seebach wrote:
> In message <ae8a916c0706092013w592e1730y59b0c9d7d55d7005@mail.gmail.com>, "huang zhimin" writes
> :
>> I want a multidimensional array with initial value 0, so I create it by
>
>> arr = Array.new(3, Array.new(3, 0)) => [[0, 0, 0], [0, 0, 0], [0, 0,
>> 0]]
>
> You have now created an array consisting of three copies of the SAME
> array [0,0,0].
>
> This is a FAQ.
>
> -s
>
>

Yeah, but at least give the guy a link to the correct solution, or
something.

This is how you do it:
arr = Array.new(3) { Array.new(3, 0) }

Dan