tags:

views:

90

answers:

1

searchterms.rb

search = "1|butter+salted|Butter salt|Butter|Túrós csusza|Margarine|Potato
          2|butter+whipped|Shea butter|Butter|Pastry bag|Cream bun|Butter cream
          3|butter+oil+anhydrous|Ultralight backpacking|Odell\'s|Ghee|Fragrance extraction|Perfume
          4|cheese+blue|Blue cheese|Shropshire Blue cheese|Buxton Blue cheese|Danish Blue cheese|Blue cheese dressing
          5|cheese+brick|Brick cheese|Oaxaca cheese|List of American cheeses|Herve cheese|Trappista cheese

.
.
.

search = search.split('\n')
catalog = String.new
a = 0; until a == search.length

  line = search[a].split('|')
  id   = line.first
  term = line[1]

  puts id + "|" + term
  puts "        1 #{line[2].nil? ? '' : line[2]}"
  puts "        2 #{line[3].nil? ? '' : line[3]}"
  puts "        3 #{line[4].nil? ? '' : line[4]}"
  puts "        4 #{line[5].nil? ? '' : line[5]}"
  puts "        5 #{line[6].nil? ? '' : line[6]}"

  choice = gets 
  choice = choice.chomp.to_i

  catalog = "#{id}|#{term}|#{line[choice]}\n"

  %x[echo "#{catalog}" >> updated_terms]

  a += 1
end

$ ruby searchterms.rb
1|butter+salted
        1 Butter salt
        2 Butter
        3 Túrós csusza
        4 Margarine
        5 Potato
2 # I don't know why this figure is printed. 
2
$

How do I get this working? I have to select the most relevant term for each food item.

I also get a strange error on setting a to any other number to start:

  searchterms.rb:7525:      
      line = search[a].split('|')
   private method `split' called for nil:NilClass (NoMethodError)
+2  A: 

You need to use

search.split("\n")

instead of

search.split('\n')

The single quotes prevent it from being interpreted as an actual newline. This is why the loop terminates -- the length of the array, not being split at all, is 1. At the end of your first iteration, a (which is 0), becomes a += 1, which is the length of the unsplit array.

Collin VanDyck
I'm so glad when it's something so simple. That solves both problems, my friend :-)
Jesse