Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Corruption when modifying array element

Reply
Thread Tools

Corruption when modifying array element

 
 
Alan Lake
Guest
Posts: n/a
 
      05-31-2007
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/.

 
Reply With Quote
 
 
 
 
yermej@gmail.com
Guest
Posts: n/a
 
      05-31-2007
On May 30, 9:34 pm, Alan Lake <(E-Mail Removed)> 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[i]}"
end
end

 
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
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Modifying every alternate element of a sequence jm.suresh@no.spam.gmail.com Python 9 11-28-2006 08:08 PM
Modifying the value of each element stored in a std::vector. Jason Heyes C++ 11 02-09-2005 08:23 PM
Re: Passing pointer to array from C to Python..and modifying same array in python? J W Python 0 07-14-2003 02:59 PM
Passing pointer to array from C to Python..and modifying same array in python? JW Python 1 07-12-2003 10:45 PM



Advertisments