Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Nested array filling bad..

Reply
Thread Tools

Nested array filling bad..

 
 
Josselin
Guest
Posts: n/a
 
      09-23-2007
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



 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      09-23-2007
Josselin wrote:
>
> 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], [], [], [], [], []]
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      09-24-2007
On Sep 23, 8:08 am, Josselin <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
Josselin
Guest
Posts: n/a
 
      09-24-2007
On 2007-09-24 06:23:59 +0200, Phrogz <(E-Mail Removed)> said:

> On Sep 23, 8:08 am, Josselin <(E-Mail Removed)> wrote:
>> 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

 
Reply With Quote
 
Josselin
Guest
Posts: n/a
 
      09-24-2007
On 2007-09-23 16:23:21 +0200, 7stud -- <(E-Mail Removed)> said:

> Josselin wrote:
>>
>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
int array filling MarsTeam C++ 0 10-15-2007 07:10 AM
Nested array filling well.. but tricky search back Josselin Ruby 0 09-24-2007 10:58 AM
filling an array excepted first and last position... Josselin Ruby 11 10-21-2006 10:17 AM
Filling 2d array in less than O(n^2) time pjhyett@gmail.com C++ 12 11-22-2005 04:46 AM
Filling a 2D array Matthew Margolis Ruby 0 09-21-2004 01:16 AM



Advertisments