checking for blank line

J

Jeff Miller

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
 
J

Justin Collins

Jeff said:
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
 
J

Jeff Miller

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
 
S

Siep Korteling

Justin said:
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
 
M

Mark Bush

Justin said:
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*/)
 
J

Justin Collins

Jeff said:
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
 
A

Arlen Cuss

[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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top