jastrachan | 22 Jul 10:05 2004

Re: Number of iteration?

On 21 Jul 2004, at 20:52, Daniel Serodio wrote:
> I'm writing a program that processes a (csv) text file. I want to skip 
> the first line, so I did:
> line = 0
> file.each { if (line++ == 0) continue; println it }
> Is there a "cleaner" way to do it?

Right now I don't think so as the each() method on file just passes in 
the String line and not a counter. The above is pretty concise though?

I guess we could add extra helper methods...

file.each { text, counter | if (counter == 0) continue; println text }

but its a lot of work to add an extra optional parameter to all the 
closure iteration methods and I think its probably less clear what this 
code does, so the way you'd done it is neater IMHO. Plus the way you'd 
done it, you could count using any type & from any index value.