Nested array filling bad..

Discussion in 'Ruby' started by Josselin, Sep 23, 2007.

  1. Josselin

    Josselin Guest

    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
     
    Josselin, Sep 23, 2007
    #1
    1. Advertising

  2. Josselin

    7stud -- Guest

    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/.
     
    7stud --, Sep 23, 2007
    #2
    1. Advertising

  3. Josselin

    Phrogz Guest

    On Sep 23, 8:08 am, Josselin <> 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.
     
    Phrogz, Sep 24, 2007
    #3
  4. Josselin

    Josselin Guest

    On 2007-09-24 06:23:59 +0200, Phrogz <> said:

    > On Sep 23, 8:08 am, Josselin <> 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
     
    Josselin, Sep 24, 2007
    #4
  5. Josselin

    Josselin Guest

    On 2007-09-23 16:23:21 +0200, 7stud -- <> 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
     
    Josselin, Sep 24, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    924
  2. rantingrick
    Replies:
    44
    Views:
    1,298
    Peter Pearson
    Jul 13, 2010
  3. Josselin
    Replies:
    0
    Views:
    112
    Josselin
    Sep 24, 2007
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    146
    Mohsen Pahlevanzadeh
    Sep 9, 2013
  5. MRAB
    Replies:
    0
    Views:
    133
Loading...

Share This Page