tags:

views:

96

answers:

1

I have to parse a tab-delimited text file with Ruby to extract some data from it. For some unknown reason some columns aren't used and are just essentially spacers; I'd like to ignore these columns since I don't need their output (however I can't just ignore all empty columns since some legitimate columns have empty values). I know the indices of these columns already (e.g. columns 6, 14, 24 and 38).

While I could just add a conditional while I'm parsing the file and say parse this unless it's one of those columns, this doesn't seem very "Rubyish" - is there a better and more elegant way to handle this? RegExps, perhaps? I thought of doing something like [6, 14, 24, 38].each { |x| columns.delete_at(x) } to remove the unused columns, but this will force me to redetermine the indices of the columns which I actually need. What I'd really like to do is just loop through the whole thing, checking the index of the current column and ignore it if it's one of the "bad" ones. However it seems very ugly to have code like unless x == 6 || x == 14 || x == 24 || x == 38

+5  A: 

No need for a massive conditional like that.

bad_cols = [6, 14, 24, 38]
columns.each_with_index do |val,idx|
  next if bad_cols.include? idx
  #process the data
end
Chuck
Cool, I had read about each_index, didn't know there was each_with_index as well. Thanks!!
Wayne M
I think what I should probably do instead of pulling the data from the file itself would be to just open it and delete the rows I don't need; I plan on loading it into a database anyways but don't want to manually remove the unused rows every time I need to load it.
Wayne M