Wierd problem reading fields in a tab-delimitted row...

K

Kurt Euler

Everyone--

A wierd problem occurs in Ruby 1.67, 1.68 and 1.8 (and probably other versions?): If I step through rows in a tab-delimited text file using code like this:

IO.foreach("wierd.txt") { |x|
field = x.chop.split("\t")
if field[4] != ""
puts "some_string"
puts field[4]
else
puts "no_string"
puts field[4]
end
}

to read each line of a text file (wierd.txt) as in this example:

string1<tab>string2<tab>string3<tab><tab><tab><tab>string4<tab><tab>
string4<tab>string5<tab>string6<tab><tab><tab><tab>string8<tab>
string9<tab>string10<tab>string11<tab><tab><tab><tab>
string12<tab>string13<tab>string14<tab><tab><tab><tab>string15


I get this output:

===================
no_string

no_string

some_string
nil
no_string
===================

The question is, why does field[4] only get assigned nil in the 3rd line but some phantom non-"nil" value in the 1st, 2nd, and 4th lines? The general case I've found is that a nil value is read from a 'column' if there are no strings in any 'columns' to the right.

Thanks if you can help!

-Kurt
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top