checking for blank line

Discussion in 'Ruby' started by Jeff Miller, Feb 27, 2008.

  1. Jeff Miller

    Jeff Miller Guest

    Hello,
    I know this is an elementary problem, but I have been fooling around
    with RoR for a little while and am now in need of doing some ruby
    scripting. I am reading a text file line by line and I want to check if
    the line is empty. For example:

    file = File.new("test.txt", "r")
    while (line = file.gets)
    @array = line.split(':')
    if @array[0] == 'cn'
    puts "#{@array[1]}"
    end
    if @array[0] == 'title'
    puts " Title: #{@array[1]}"
    end
    if @array[0] == 'company'
    puts " Company: #{@array[1]}"
    end
    if @array[0] == nil #~ THIS IS THE LINE I'M TALKING
    ABOUT
    puts "-----"
    end
    end
    file.close

    The input file just looks like this:

    cn: John Smith
    title: executive
    company: ReallyCoolSoftware, inc.

    cn: John Doe
    title: janitor
    company: NotSoCoolSoftware, inc.

    etc...

    How can I pick up those blank lines in my IF statement? I tried nil and
    '', but neither seem to work.

    Any help is appreciated!
    Thank you!!
    - Jeff
    --
    Posted via http://www.ruby-forum.com/.
    Jeff Miller, Feb 27, 2008
    #1
    1. Advertising

  2. Jeff Miller wrote:
    > Hello,
    > I know this is an elementary problem, but I have been fooling around
    > with RoR for a little while and am now in need of doing some ruby
    > scripting. I am reading a text file line by line and I want to check if
    > the line is empty. For example:
    >
    > file = File.new("test.txt", "r")
    > while (line = file.gets)
    > @array = line.split(':')
    > if @array[0] == 'cn'
    > puts "#{@array[1]}"
    > end
    > if @array[0] == 'title'
    > puts " Title: #{@array[1]}"
    > end
    > if @array[0] == 'company'
    > puts " Company: #{@array[1]}"
    > end
    > if @array[0] == nil #~ THIS IS THE LINE I'M TALKING
    > ABOUT
    > puts "-----"
    > end
    > end
    > file.close
    >
    > The input file just looks like this:
    >
    > cn: John Smith
    > title: executive
    > company: ReallyCoolSoftware, inc.
    >
    > cn: John Doe
    > title: janitor
    > company: NotSoCoolSoftware, inc.
    >
    > etc...
    >
    > How can I pick up those blank lines in my IF statement? I tried nil and
    > '', but neither seem to work.
    >
    > Any help is appreciated!
    > Thank you!!
    > - Jeff
    >



    Remember, "blank" lines might not be truly empty:
    irb(main):001:0> a = gets

    => "\n"
    irb(main):002:0> a.split(":")
    => ["\n"]

    Also, using split on an empty string is not going to give you nil, but
    an empty array:

    irb(main):003:0> "".split(":")
    => []

    If you don't want the line endings in your strings, try using

    while (line = file.gets.chomp)

    or

    while (line = file.gets.strip)


    instead.

    -Justin
    Justin Collins, Feb 27, 2008
    #2
    1. Advertising

  3. Jeff Miller

    Trans Guest

    if /\S/ !~ line # is blank
    Trans, Feb 27, 2008
    #3
  4. Jeff Miller

    Jeff Miller Guest

    Thanks for the reply!

    That does make sense! However, I still can't get this to work properly.
    I tried to use strip and chomp, but both of them kicked me errors
    "undefined method 'chomp' called for nil:NilClass (NoMethodError)". I
    figure this is kind of odd, since I've used both of these in RoR before.
    FYI, the code I posted is the entire script... perhaps I'm missing some
    sort of 'require' statement or something?

    Anyhow, I tried to use the \n, but I can't get to work either... I also
    tried @array[0] == '\n' and a bunch of other little changes. How may I
    construct an IF statement with this?

    Thanks again,
    - Jeff
    --
    Posted via http://www.ruby-forum.com/.
    Jeff Miller, Feb 27, 2008
    #4
  5. Justin Collins wrote:
    > Jeff Miller wrote:

    (...)>
    > Also, using split on an empty string is not going to give you nil, but
    > an empty array:
    >
    > irb(main):003:0> "".split(":")
    > => []
    >


    Yes, but array[0] will return nil if array is empty, so the OP is doing
    ok.

    > If you don't want the line endings in your strings, try using
    >
    > while (line = file.gets.chomp)


    This is too early, it will error when you get to the end of the file.
    One line later is fine:

    @array = line.chomp.split(':')

    > or
    >
    > while (line = file.gets.strip)
    >
    >
    > instead.
    >
    > -Justin



    Regards,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
    Siep Korteling, Feb 27, 2008
    #5
  6. Jeff Miller

    Jeff Miller Guest

    Jeff Miller, Feb 27, 2008
    #6
  7. Jeff Miller

    Mark Bush Guest

    Justin Collins wrote:
    > while (line = file.gets.strip)


    This will fail at eof as then file.gets will return nil. The strip
    should be put into the loop.

    You can also heavily refactor:

    file = File.new("test.txt", "r")
    while (line = file.gets)
    line.strip!
    if line.empty?
    puts "-----"
    else
    @array = line.split(':')
    if m = @array[0].match(/(title|company)/)
    print " #{m[1].capitalize}: "
    end
    puts "#{@array[1]}"
    end
    end
    file.close

    Also, since your input seems to have spaces after the colons, you may
    want to ignore those. If so, replace the split with:

    @array = line.split(/\s*:\s*/)

    --
    Posted via http://www.ruby-forum.com/.
    Mark Bush, Feb 27, 2008
    #7
  8. Jeff Miller wrote:
    > Thanks for the reply!
    >
    > That does make sense! However, I still can't get this to work properly.
    > I tried to use strip and chomp, but both of them kicked me errors
    > "undefined method 'chomp' called for nil:NilClass (NoMethodError)". I
    > figure this is kind of odd, since I've used both of these in RoR before.
    > FYI, the code I posted is the entire script... perhaps I'm missing some
    > sort of 'require' statement or something?
    >


    Ah, good point. That happens on the last loop through, when the file is
    empty. My apologies.

    > Anyhow, I tried to use the \n, but I can't get to work either... I also
    > tried @array[0] == '\n' and a bunch of other little changes. How may I
    > construct an IF statement with this?
    >
    > Thanks again,
    > - Jeff
    >


    Here's a way, with some other changes:

    File.open("test.txt", "r") do |file|
    while (line = file.gets)
    array = line.split(':')
    if array[0] == 'cn'
    puts array[1]
    elsif array[0] == 'title'
    puts " Title: #{array[1]}"
    elsif array[0] == 'company'
    puts " Company: #{array[1]}"
    elsif line.strip.empty?
    puts "-----"
    end
    end
    end


    --------------

    File.open do |f| ... end will make sure the file gets closed for you.
    Using @array is not necessary, as it just needs to be a local variable, not an instance variable.
    String interpolation is not necessary to print an element of the array.
    line.strip will get rid of any whitespace and then you can check if the string is empty.
    Also, note what happens when you get a line that is neither empty nor matches any of the above.
    You can use a series of elsifs to make it slightly shorter/faster/easier to read.

    -Justin
    Justin Collins, Feb 27, 2008
    #8
  9. Jeff Miller

    Arlen Cuss Guest

    [Note: parts of this message were removed to make it a legal post.]

    Might I suggest avoiding such control structures:

    File.open("test.txt", "r").each_line do |line|
    array = line.strip.split(':')
    case array[0]
    when 'cn'
    puts array[1]
    when 'title'
    puts " Title: #{array[1]}"
    when 'company'
    puts " Company: #{array[1]}"
    else
    puts "-----" if line.strip.empty?
    end
    end.close

    This may or may not be ideal for you. You may also want to map the array
    like .map {|e| e.strip}, since extra spaces may be included.

    Arlen
    Arlen Cuss, Feb 28, 2008
    #9
    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. Alan Connor
    Replies:
    0
    Views:
    3,038
    Alan Connor
    May 7, 2005
  2. Hugo
    Replies:
    10
    Views:
    1,293
    Matt Humphrey
    Oct 18, 2004
  3. kaushikshome
    Replies:
    4
    Views:
    755
    kaushikshome
    Sep 10, 2006
  4. Replies:
    7
    Views:
    331
    Andrew Lee
    Apr 21, 2008
  5. scad
    Replies:
    23
    Views:
    1,146
    Alf P. Steinbach
    May 17, 2009
Loading...

Share This Page