# Trouble with Pushing Arrays to Arrays

**URL:** <https://www.ruby-forum.com/t/trouble-with-pushing-arrays-to-arrays/187125>\
**Category:** Ruby\
**Created:** [April 17, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/trouble-with-pushing-arrays-to-arrays/187125 "2010-04-17T16:38:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 17, 2010, 4:38pm UTC](https://www.ruby-forum.com/t/trouble-with-pushing-arrays-to-arrays/187125/1 "2010-04-17T16:38:43Z")

</div>

Hello everyone! I’m having some trouble with pushing an array to another  
array. Maybe this is an easy fix that I’m just not seeing? Here’s my  
code:

require ‘nokogiri’  
require ‘open-uri’

url = ‘C:\users\derek\desktop\Schedule2.html’  
doc = Nokogiri::HTML(open(url))

raw\_course\_list = Array.new  
temp = Array.new

doc.css(“tr”).each { |row| # Search through every row  
row.css(“td”).each { |column|  
temp.push(column.text.strip)  
}  
# puts temp.class #=\> Array  
# puts temp.size #=\> 20  
# puts temp[7] # E.g. =\> “Intro to Financial Accounting”  
raw\_course\_list.push(temp)  
# puts "raw\_course\_list[0]: " + raw\_course\_list[0].to\_s # \*\*\*\*  
Always returns the last temp pushed, not what should be at [0] (the  
first array ever pushed) \*\*\*\*  
# print "raw\_course\_list.size: ", raw\_course\_list.size, “\n” #  
Correctly increases +1 each push  
temp.clear  
}

puts raw\_course\_list.each { |i| puts i.to\_s} # \*\*\*\* Prints out line  
after line of empty arrays \*\*\*\*

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 17, 2010, 4:42pm UTC](https://www.ruby-forum.com/t/trouble-with-pushing-arrays-to-arrays/187125/2 "2010-04-17T16:42:54Z")

</div>

Oh and a brief explanation:

The code goes through HTML containing courses listed in tables, where  
each table has 20 columns of data. This segment of code ultimately is  
supposed to create an array of arrays – i.e. each element in  
raw\_course\_list is an array containing each of the 20 columns in one row  
of the HTML file.

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 17, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/trouble-with-pushing-arrays-to-arrays/187125/3 "2010-04-17T16:47:06Z")

</div>

And one last thing: As you can see from the commented out code, the temp  
array is working as intended – every time the row.css(“td”).each  
completes, it’s filled with data from all the column in the HTML… The  
problem occurs (I’m guessing) when I push temp to raw\_course\_list.

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 17, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/trouble-with-pushing-arrays-to-arrays/187125/4 "2010-04-17T16:59:33Z")

</div>

> The problem is that you’re reusing temp, and clearing it.

Doh! I knew it was something easy. The raw\_course\_list is pushing a  
pointer to temp, not creating a copy of temp to push. Is this correct?

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 17, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/trouble-with-pushing-arrays-to-arrays/187125/5 "2010-04-17T17:02:42Z")

</div>

> raw\_course\_list = doc.css(“tr”).map { |row|  
> row.css(“td”).map { |column| column.text.strip }  
> }

Also, this coding worked perfectly. It’s very elegant; thanks! I had to  
look up map in the API to fully understand what was going on there 🙂

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 17, 2010, 5:35pm UTC](https://www.ruby-forum.com/t/trouble-with-pushing-arrays-to-arrays/187125/6 "2010-04-17T17:35:36Z")

</div>

Hi –

On Sat, 17 Apr 2010, Derek C. wrote:

> > The problem is that you’re reusing temp, and clearing it.
> 
> Doh! I knew it was something easy. The raw\_course\_list is pushing a  
> pointer to temp, not creating a copy of temp to push. Is this correct?

Yes, essentially, though the more standard word in Ruby is  
“reference”.

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally  
COMPLEAT Coming to Chicago area, June 18-19, 2010!  
RUBYIST [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [April 17, 2010, 4:55pm UTC](https://www.ruby-forum.com/t/trouble-with-pushing-arrays-to-arrays/187125/7 "2010-04-17T16:55:12Z")

</div>

Hi –

On Sat, 17 Apr 2010, Derek C. wrote:

> raw\_course\_list = Array.new  
> # puts "raw\_course\_list[0]: " + raw\_course\_list[0].to\_s # \*\*\*\*  
> Always returns the last temp pushed, not what should be at [0] (the  
> first array ever pushed) \*\*\*\*  
> # print "raw\_course\_list.size: ", raw\_course\_list.size, “\n” #  
> Correctly increases +1 each push  
> temp.clear  
> }
> 
> puts raw\_course\_list.each { |i| puts i.to\_s} # \*\*\*\* Prints out line  
> after line of empty arrays \*\*\*\*

The problem is that you’re reusing temp, and clearing it.

temp = []  
result = []

%w{ one two three }.each do |word|  
temp.push(word)  
result.push(temp)  
temp.clear  
end

p result # =\> [[], [], []]  
p result.map {|obj| obj.object\_id } # =\> [606420, 606420, 606420]

I end up with three copies of temp inside result, and temp is empty.

Try creating the temp array inside the loop, and not clearing it. Or  
let Ruby do more of the work:

raw\_course\_list = doc.css(“tr”).map { |row|  
row.css(“td”).map { |column| column.text.strip }  
}

(if I’ve got the logic right – if not, tweak as needed 🙂

David
