Corruption when modifying array element

Discussion in 'Ruby' started by Alan Lake, May 31, 2007.

  1. Alan Lake

    Alan Lake Guest

    When modifying an element in a row, the result becomes corrupted when I
    try to read it back. new_row is being modified correctly. It is then
    being appended to new_rows (plural). But when I read new_rows back,
    each element contains only the last new_row created. What gives?

    $ cat test.rb;./test.rb
    #!/usr/bin/env ruby

    def test1
    row = [ 'field1' , 'field2', 'field3' ]
    test = [ 'test1', 'test2', 'test3' ]
    new_rows = Array.new
    new_row = row
    test.each { |value|
    new_row[2] = value
    puts "new_row: #{new_row}"
    new_rows << new_row
    }
    puts
    for ixt in 0..new_rows.size - 1
    puts "new_rows[#{ixt}]: #{new_rows[ixt]}"
    end
    end
    test1

    new_row: field1field2test1
    new_row: field1field2test2
    new_row: field1field2test3

    new_rows[0]: field1field2test3
    new_rows[1]: field1field2test3
    new_rows[2]: field1field2test3

    --
    Posted via http://www.ruby-forum.com/.
    Alan Lake, May 31, 2007
    #1
    1. Advertising

  2. Alan Lake

    Guest

    On May 30, 9:34 pm, Alan Lake <> wrote:
    > When modifying an element in a row, the result becomes corrupted
    > def test1
    > row = [ 'field1' , 'field2', 'field3' ]
    > test = [ 'test1', 'test2', 'test3' ]
    > new_rows = Array.new
    > new_row = row


    At this point, new_row is the same object as row -- it is basically a
    reference to the Array object you created on the first line of test1.

    > test.each { |value|
    > new_row[2] = value
    > puts "new_row: #{new_row}"
    > new_rows << new_row


    Everytime you do this, you are putting a reference to (new_)row into
    your new array. All three references refer to the same Array object,
    which you created on the first line. If you need to do something like
    this, you can use Array's clone method to create a copy of the array.
    It will be a new object:

    arr1 = [1, 2, 3]
    arr2 = arr1
    arr3 = arr1.clone

    puts arr1.object_id
    => 24377410
    puts arr2.object_id
    => 24377410
    puts arr3.object_id
    => 24324410

    So, try:

    def test1
    row = [ 'field1' , 'field2', 'field3' ]
    test = [ 'test1', 'test2', 'test3' ]
    new_rows = []
    test.each { |value|
    new_row = row.clone
    new_row[2] = value
    puts "new_row: #{new_row}"
    new_rows << new_row
    }

    new_rows.each_with_index do |v, i|
    puts "new_rows[#{i}]: #{new_rows}"
    end
    end
    , May 31, 2007
    #2
    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. Robin Emery
    Replies:
    5
    Views:
    714
  2. JW
    Replies:
    1
    Views:
    444
    David Jeske
    Jul 12, 2003
  3. Ike
    Replies:
    3
    Views:
    486
    Dale King
    Nov 29, 2006
  4. Active8

    Array corruption

    Active8, Aug 20, 2007, in forum: C++
    Replies:
    6
    Views:
    463
    Active8
    Aug 22, 2007
  5. HANM
    Replies:
    2
    Views:
    709
    Joseph Kesselman
    Jan 29, 2008
Loading...

Share This Page