[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Nested arrays - what can be wrong with this?

Tim L

6/20/2006 7:01:00 AM

Can anyone explain why this code:

a = Array.new(3,Array.new(3,0))
a[1][1]=1
a.each {|r| puts r.join(', ')}

generates this output?

0, 1, 0
0, 1, 0
0, 1, 0

I was expecting this

0, 0, 0
0, 1, 0
0, 0, 0

Something wrong with my understanding of Ruby - or conceivably the set up on
my PC?

Tim L


5 Answers

unknown

6/20/2006 8:25:00 AM

0

When you use Array#new, this does create an array of size '3', but with =
a =

reference to the same object.
From the docs:
--
"new Array.new( anInteger=3D0, anObject=3Dnil ) -> anArray

Returns a new array, optionally with a size and initial value (that is, =
=

anInteger references to the same anObject).
--
You can verify this by doing:
a.each {|r| puts r.object_id}

(All of the object ids are the same, showing they're referencing the sam=
e =

object).

So, you just need to construct your multidimensional array differently. =
=

Maybe...

a =3D Array.new
0.upto(2) {|i| a[i] =3D Array.new(3, 0)}
a[1][1] =3D 1

a.each {|r| puts r.join(', ')}

That should give you your
0, 0, 0
0, 1, 0
0, 0, 0


Of course, there's probably a better way to do it..

On Tue, 20 Jun 2006 00:01:26 -0700, Tim L =

<tim.anti@spam.lund-tvam.demon.co.uk> wrote:

> Can anyone explain why this code:
>
> a =3D Array.new(3,Array.new(3,0))
> a[1][1]=3D1
> a.each {|r| puts r.join(', ')}
>
> generates this output?
>
> 0, 1, 0
> 0, 1, 0
> 0, 1, 0
>
> I was expecting this
>
> 0, 0, 0
> 0, 1, 0
> 0, 0, 0
>
> Something wrong with my understanding of Ruby - or conceivably the set=
=

> up on
> my PC?
>
> Tim L
>
>

Daniel Schierbeck

6/20/2006 9:51:00 AM

0

Tim L wrote:
> Can anyone explain why this code:
>
> a = Array.new(3,Array.new(3,0))
> a[1][1]=1
> a.each {|r| puts r.join(', ')}
>
> generates this output?
>
> 0, 1, 0
> 0, 1, 0
> 0, 1, 0

Try this:

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


Cheers,
Daniel

marek4130

6/20/2006 10:05:00 AM

0

Hi Tim,

The Array.new(3,0) is called only once. so a has 3 references to the
same array.
a[0] == a[1] will return true as will a[1] == a[2]

Instead try:
a = Array.new(3) {Array.new(3,0)}

have fun,
Marek
Tim L wrote:
> Can anyone explain why this code:
>
> a = Array.new(3,Array.new(3,0))
> a[1][1]=1
> a.each {|r| puts r.join(', ')}
>
> generates this output?
>
> 0, 1, 0
> 0, 1, 0
> 0, 1, 0
>
> I was expecting this
>
> 0, 0, 0
> 0, 1, 0
> 0, 0, 0
>
> Something wrong with my understanding of Ruby - or conceivably the set up on
> my PC?
>
> Tim L

Jeff Schwab

6/20/2006 2:35:00 PM

0

Stephen Gustafson wrote:
> When you use Array#new, this does create an array of size '3', but with
> a reference to the same object.
> From the docs:
> --
> "new Array.new( anInteger=0, anObject=nil ) -> anArray
>
> Returns a new array, optionally with a size and initial value (that is,
> anInteger references to the same anObject).
> --

It looks like the description in the ri entry mistakenly refers to the
first, second, and third versions of Array#new as the second, third, and
fourth versions, respectively. Where does one go to report or amend ri
entries? I tried to check the Documentation FAQ from ruby-lang.org, but
the link (to rubygarden.org) seems to be broken.

------------------------------------------------------------- Array::new
Array.new(size=0, obj=nil)
Array.new(array)
Array.new(size) {|index| block }
------------------------------------------------------------------------
Returns a new array. In the first form, the new array is empty. In
the second it is created with _size_ copies of _obj_ (that is,
_size_ references to the same _obj_). The third form creates a copy
of the array passed as a parameter (the array is generated by
calling to_ary on the parameter). In the last form, an array of the
given size is created. Each element in this array is calculated by
passing the element's index to the given block and storing the
return value.

Tim L

6/20/2006 3:20:00 PM

0

Thank you guys
<marek4130@gmail.com> wrote in message
news:1150797900.845166.49370@h76g2000cwa.googlegroups.com...
> Hi Tim,
>
> The Array.new(3,0) is called only once. so a has 3 references to the
> same array.
> a[0] == a[1] will return true as will a[1] == a[2]
>
> Instead try:
> a = Array.new(3) {Array.new(3,0)}
>
> have fun,
> Marek
> Tim L wrote:
>> Can anyone explain why this code:
>>
>> a = Array.new(3,Array.new(3,0))
>> a[1][1]=1
>> a.each {|r| puts r.join(', ')}
>>
>> generates this output?
>>
>> 0, 1, 0
>> 0, 1, 0
>> 0, 1, 0
>>
>> I was expecting this
>>
>> 0, 0, 0
>> 0, 1, 0
>> 0, 0, 0
>>
>> Something wrong with my understanding of Ruby - or conceivably the set up
>> on
>> my PC?
>>
>> Tim L
>