Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > FasterCSV.foreach lineno?

Reply
Thread Tools

FasterCSV.foreach lineno?

 
 
Bil Kleb
Guest
Posts: n/a
 
      04-24-2008
OK, so I can't think my way out of a paper this morning.

Can I access FasterCVS#lineno while inside a foreach?

For example,

$ cat << EOF > csv_file
one
two
three
EOF

$ ruby -rubygems \
-e'require "fastercsv";FasterCSV.foreach("csv_file")\
{|r| puts "lineno somehow"}'

Thanks,
--
Bil Kleb
http://fun3d.larc.nasa.gov
 
Reply With Quote
 
 
 
 
Rob Biedenharn
Guest
Posts: n/a
 
      04-24-2008
On Apr 24, 2008, at 7:55 AM, Bil Kleb wrote:

> OK, so I can't think my way out of a paper this morning.
>
> Can I access FasterCVS#lineno while inside a foreach?
>
> For example,
>
> $ cat << EOF > csv_file
> one
> two
> three
> EOF
>
> $ ruby -rubygems \
> -e'require "fastercsv";FasterCSV.foreach("csv_file")\
> {|r| puts "lineno somehow"}'
>
> Thanks,
> --
> Bil Kleb
> http://fun3d.larc.nasa.gov


Bil,

Since it looks like the answer to your first question is "No.", what
if you defined:

class FasterCSV
def self.foreach_with_lineno(path, options = Hash.new, &block)
open(path, options) do |csv|
csv.each {|line| block[line, csv.lineno]}
end
end
end

irb> FasterCSV.foreach_with_lineno("csv_file") {|r,l| puts "#{l}: #{r}"}
1: one
2: two
3: three
=> nil

-Rob

Rob Biedenharn http://agileconsultingllc.com
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
James Gray
Guest
Posts: n/a
 
      04-24-2008
On Apr 24, 2008, at 6:55 AM, Bil Kleb wrote:

> OK, so I can't think my way out of a paper this morning.
>
> Can I access FasterCVS#lineno while inside a foreach?


No, but you can switch to an each() iterator and get at it that way
(just as you would with an IO object):

$ cat data.csv
one
two
three
$ cat fcsv_example.rb
#!/usr/bin/env ruby -wKU

require "rubygems"
require "faster_csv"

FCSV.open("data.csv") do |csv|
csv.each do |row|
puts "#{csv.lineno}: #{row}"
end
end
$ ruby fcsv_example.rb
1: one
2: two
3: three

Hope that helps.

James Edward Gray II

 
Reply With Quote
 
Bil
Guest
Posts: n/a
 
      04-24-2008
On Apr 24, 9:07*am, James Gray <(E-Mail Removed)> wrote:
>
> No, but you can switch to an each() iterator and get at it that way *
> [..]
> Hope that helps.


Yes, thanks.

Regards,
--
http://twitter.com/bil_kleb

P.S. My comp.lang.ruby newsfeed didn't carry either of your replies.
 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      04-24-2008
On Apr 24, 2008, at 10:25 AM, Bil wrote:

> P.S. My comp.lang.ruby newsfeed didn't carry either of your replies.


They seem to have reached Usenet OK:

http://groups.google.com/group/comp....402264883f615#

James Edward Gray II


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments