Nested array filling bad..

J

Josselin

I try to fill a nested array this way

irb(main):001:0> cat_a = Array.new(7, Array.new)
irb(main):002:0> cat_a[0] << 0
=> [0]
irb(main):004:0> cat_a[1] << 1
=> [0, 1]

but it gives me back
irb(main):005:0> cat_a
=> [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]

I would like to get back

[ [0], [1], [], [], [], [], [] ]

and adding cat_a[0] << 9
would give

[ [0, 9], [1], [], [], [], [], [] ]

what's wrong with my array def ?

thanks
 
7

7stud --

Josselin said:
what's wrong with my array def ?

--------------------------------------- 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_).

So, you are essentially doing this:

a = Array.new
b = a
p a, b

--output:--
[]
[]


a << 1
p a, b

--output:--

[1]
[1]


Try this instead:

arr = Array.new(7) {Array.new}
arr[0] << 0
arr[1] << 1

p arr

--output:--
[[0], [1], [], [], [], [], []]
 
P

Phrogz

irb(main):001:0> cat_a = Array.new(7, Array.new)
irb(main):002:0> cat_a[0] << 0
=> [0]
irb(main):004:0> cat_a[1] << 1
=> [0, 1]

but it gives me back
irb(main):005:0> cat_a
=> [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]

I would like to get back

[ [0], [1], [], [], [], [], [] ]

irb(main):001:0> cat_a = Array.new(7){ [] }
=> [[], [], [], [], [], [], []]
irb(main):002:0> cat_a[0] << 0
=> [0]
irb(main):003:0> cat_a[1] << 1
=> [1]
irb(main):004:0> cat_a
=> [[0], [1], [], [], [], [], []]


What's wrong with it is that you are using the exact same array for
all 7 spots. You want a new Array to be created for each spot, using
the block notation above.
 
J

Josselin

irb(main):001:0> cat_a = Array.new(7, Array.new)
irb(main):002:0> cat_a[0] << 0
=> [0]
irb(main):004:0> cat_a[1] << 1
=> [0, 1]

but it gives me back
irb(main):005:0> cat_a
=> [[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]]

I would like to get back

[ [0], [1], [], [], [], [], [] ]

irb(main):001:0> cat_a = Array.new(7){ [] }
=> [[], [], [], [], [], [], []]
irb(main):002:0> cat_a[0] << 0
=> [0]
irb(main):003:0> cat_a[1] << 1
=> [1]
irb(main):004:0> cat_a
=> [[0], [1], [], [], [], [], []]


What's wrong with it is that you are using the exact same array for
all 7 spots. You want a new Array to be created for each spot, using
the block notation above.

thanks a lot.. got it
 
J

Josselin

Josselin said:
what's wrong with my array def ?

--------------------------------------- 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_).

So, you are essentially doing this:

a = Array.new
b = a
p a, b

--output:--
[]
[]


a << 1
p a, b

--output:--

[1]
[1]


Try this instead:

arr = Array.new(7) {Array.new}
arr[0] << 0
arr[1] << 1

p arr

--output:--
[[0], [1], [], [], [], [], []]

thanks a lot.. got it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top