# Ruby looping

**URL:** <https://www.ruby-forum.com/t/ruby-looping/194412>\
**Category:** Ruby\
**Created:** [September 4, 2010, 12:38pm UTC](https://www.ruby-forum.com/t/ruby-looping/194412 "2010-09-04T12:38:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [September 4, 2010, 12:38pm UTC](https://www.ruby-forum.com/t/ruby-looping/194412/1 "2010-09-04T12:38:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [September 4, 2010, 2:07pm UTC](https://www.ruby-forum.com/t/ruby-looping/194412/2 "2010-09-04T14:07:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/ruby-looping/194412/3 "2010-09-06T16:39:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [September 6, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/ruby-looping/194412/4 "2010-09-06T16:50:53Z")

</div>

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

> >
