FasterCSV.foreach lineno?

Discussion in 'Ruby' started by Bil Kleb, Apr 24, 2008.

  1. Bil Kleb

    Bil Kleb Guest

    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 Kleb, Apr 24, 2008
    #1
    1. Advertising

  2. 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
    Rob Biedenharn, Apr 24, 2008
    #2
    1. Advertising

  3. Bil Kleb

    James Gray Guest

    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
    James Gray, Apr 24, 2008
    #3
  4. Bil Kleb

    Bil Guest

    On Apr 24, 9:07 am, James Gray <> 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.
    Bil, Apr 24, 2008
    #4
  5. Bil Kleb

    James Gray Guest

    James Gray, Apr 24, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy

    ply yacc lineno not working?

    Laszlo Nagy, May 16, 2008, in forum: Python
    Replies:
    0
    Views:
    429
    Laszlo Nagy
    May 16, 2008
  2. Laszlo Nagy

    Re: ply yacc lineno not working?

    Laszlo Nagy, May 16, 2008, in forum: Python
    Replies:
    1
    Views:
    495
    inhahe
    May 16, 2008
  3. Hellmut Weber
    Replies:
    14
    Views:
    522
    Peter Otten
    Mar 31, 2010
  4. Belorion
    Replies:
    4
    Views:
    88
    Brian Schröder
    Nov 18, 2005
  5. Dot Baiki

    FasterCSV.foreach loop

    Dot Baiki, Sep 13, 2009, in forum: Ruby
    Replies:
    15
    Views:
    271
    James Edward Gray II
    Jan 11, 2011
Loading...

Share This Page