Ruby looping

Hey,

I have a quick query regarding loops, I am reading a CSV file take the
information from the first line of the CSV file and calling an object
which does some stuff, then I want to move on to the next line in the
CSV file and continue this process until I have done every line. My code
is as follows:

require ‘csv’

file = “H:\13936 _ Project HS\extra tiff work\test\rename.csv”
#path to CSV file

def renameFile(oldID, newID)
DO SOME STUFF
end

d = CSV.open(FILE, “r”)
#Open the as CSV
d.each do |setA, setB|
#2 columns of data
next
oldID = setB.to_s.strip
newID = setA.to_s.strip[/.*(?=…+$)/]
renameFile(oldID, newID)
#Call our object
end
end

The above code does what I want it to but it very slow and gets slower
the bigger the CSV file, because it seems to go through each line every
time we are in the loop, when I only want it to go through the next line
in the CSV file. I basically need it to do:

line A
CALL OBJECT
skip line A do line B
CALL OBJECT
skip line A and line B do line C
CALL OBJECT
etc etc

Any help is appreciated.

Stuart C. wrote:

d = CSV.open(FILE, “r”)
#Open the as CSV
d.each do |setA, setB|
#2 columns of data
next
oldID = setB.to_s.strip
newID = setA.to_s.strip[/.*(?=…+$)/]
renameFile(oldID, newID)
#Call our object
end
end

Does this really work at all? Doesn’t ‘next’ just skip every line as
soon as you’ve read it?

Otherwise, ‘each’ should do what you want: present you with one line at
a time in setA and setB. Add a “puts” line inside the each block to see
if that’s what’s happening:

puts “Now processing #{setA.inspect},#{setB.inspect}”

If you’re using 1.8, you may find FasterCSV works better - it’s
available as a gem. It’s standard in 1.9.

On 04.09.2010 12:39, Stuart C. wrote:

I have a quick query regarding loops, I am reading a CSV file take the
information from the first line of the CSV file and calling an object

We “call a method” or “invoke a method” but we do not “call an object” -
at least not if we want to convey that we invoke a method of that
object.

which does some stuff, then I want to move on to the next line in the
CSV file and continue this process until I have done every line. My code
is as follows:

require ‘csv’

file = “H:\13936 _ Project HS\extra tiff work\test\rename.csv”
#path to CSV file

def renameFile(oldID, newID)
DO SOME STUFF
end

d = CSV.open(FILE, “r”)
#Open the as CSV
d.each do |setA, setB|
#2 columns of data
next
oldID = setB.to_s.strip
newID = setA.to_s.strip[/.*(?=…+$)/]
renameFile(oldID, newID)
#Call our object
end
end

The above code does what I want it to but it very slow and gets slower
the bigger the CSV file, because it seems to go through each line every
time we are in the loop,

Are you sure? The code looks OK because you do only one iteration with
#each on d. (Btw, you can also do CSV.foreach(file) IIRC.) You should
be able to easily verify by printing the record in the line with your
comment “2 columns of data”.

when I only want it to go through the next line
in the CSV file. I basically need it to do:

line A
CALL OBJECT
skip line A do line B
CALL OBJECT
skip line A and line B do line C
CALL OBJECT
etc etc

Any help is appreciated.

Since we cannot see what happens in “Call our object” we cannot have an
idea where your program spends its time. You could try to run with the
built in profiler (just do “ruby -r profile”).

Kind regards

robert

Sorry, the next was commented basically just to say move to the next so
that was a typo.

I tried faster CSV and it improved speed.

Thanks

Stuart