looping CSV.foreach??

  • Thread starter Kamarulnizam Rahim
  • Start date
K

Kamarulnizam Rahim

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
 
J

James Edward Gray II

I use the following codes:
=20
CSV.foreach('ActionPlan.csv') do |row|
next unless row[0] =3D~ /2.[a-z]/
t =3D Target.new
t.question =3D row[1]
pp t.question
end
=20
And get the following results:
=20
"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",
=20
My question is how do i collect all the results and put it into one
variable?

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

targets =3D [ ]

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=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top