On 04.09.2010 12:39, Stuart Clarke 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

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/