Looping CSV.foreach?

I use the following codes:

CSV.foreach('ActionPlan.csv') do |row|
  next unless row[0] =~ /2.[a-z]/
  t = Target.new
  t.question = row[1]
  pp t.question
end

And get the following results:

"Routinely monitor energy use",
"Identify major energy uses",
"Improve inspection and maintenance procedures",
"Routinely check environmental performance/compliance",
"Routinely inspect your site for problems",
"Monitor feedback on the environmental performance of your
products/services",
"Improve environmental performance of your products/services",

My question is how do i collect all the results and put it into one
variable?

Nizam

···

--
Posted via http://www.ruby-forum.com/.

You would collect them in an Array. Put a line like this before the foreach loop:

  targets =

Then inside the foreach(), you could add to it:

  targets << t

Finally, you can make use of it after the foreach() loop.

James Edward Gray II

···

On Jan 31, 2011, at 10:51 PM, Kamarulnizam Rahim wrote:

I use the following codes:

CSV.foreach('ActionPlan.csv') do |row|
next unless row[0] =~ /2.[a-z]/
t = Target.new
t.question = row[1]
pp t.question
end

And get the following results:

"Routinely monitor energy use",
"Identify major energy uses",
"Improve inspection and maintenance procedures",
"Routinely check environmental performance/compliance",
"Routinely inspect your site for problems",
"Monitor feedback on the environmental performance of your
products/services",
"Improve environmental performance of your products/services",

My question is how do i collect all the results and put it into one
variable?