Nested arrays - what can be wrong with this?

Discussion in 'Ruby' started by Tim L, Jun 20, 2006.

  1. Tim L

    Tim L Guest

    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
    Tim L, Jun 20, 2006
    #1
    1. Advertising

  2. 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
    Daniel Schierbeck, Jun 20, 2006
    #2
    1. Advertising

  3. Tim L

    Guest

    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
    , Jun 20, 2006
    #3
  4. Tim L

    Tim L Guest

    Thank you guys
    <> wrote in message
    news:...
    > 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

    >
    Tim L, Jun 20, 2006
    #4
    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. Russ Perry Jr
    Replies:
    2
    Views:
    4,099
    Russ Perry Jr
    Aug 20, 2004
  2. Chad E. Dollins
    Replies:
    3
    Views:
    648
    Kai-Uwe Bux
    Nov 8, 2005
  3. request@no_spam.com
    Replies:
    5
    Views:
    414
  4. Ultrus
    Replies:
    3
    Views:
    362
    Stefan Behnel
    Jul 9, 2007
  5. Philipp
    Replies:
    21
    Views:
    1,105
    Philipp
    Jan 20, 2009
Loading...

Share This Page