title: How can I iterate through a CSV file in RUBY to produce an HTML unordered list?
body: <p>I am trying to write a script that will iterate through a CSV file in Ruby that will produce a multi-level unordered list.</p>
  
  <p>The code I have now doesn't cut off the <code>&lt;ul&gt;</code> in the right spots. The output just continues to new levels.  </p>
  
  <p>Here is the Ruby code: </p>
  
  <pre><code>require 'csv'
  
  col_data = [] 
  CSV.foreach("primary_NAICS_code.txt") {|row| col_data &lt;&lt; row} 
  
  begin
    file = File.open("primary_NAICS_code.html", "w")
    file.write("&lt;ul&gt;\n")
    depth = 1
    col_data.each do |row|
      indentation, (text,*) = row.slice_before(String).to_a
  
      if indentation.length &gt; depth 
        file.write("&lt;ul&gt;\n")
      elsif indentation.length &lt; depth 
        file.write("&lt;/ul&gt;\n")
      end
  
      if text.index(/^\d{2}[:]/)
        file.write("&lt;li&gt;" +text+ "&lt;/li&gt;&lt;/ul&gt;\n")
      else  
        file.write("&lt;li&gt;" +text+ "&lt;/li&gt;\n")
      end
      depth = indentation.length
  
    end
    file.write("&lt;/ul&gt;\n")
  rescue IOError =&gt; e
    puts e
  ensure
    file.close unless file == nil
  end
  </code></pre>
  
  <p>This is the output I'm getting:</p>
  
  <pre><code>11: Agriculture, Forestry, Fishing and Hunting
    111: Crop Production
      111110: Soybean Farming
      111120: Oilseed (except Soybean) Farming
      111130: Dry Pea and Bean Farming
      .
      .
      .
    112: Animal Production
      112111: Beef Cattle Ranching and Farming
      112112: Cattle Feedlots
      .
      .
      .
    21: Mining&lt;~~(there should be a &lt;/ul&gt; here)
     211: Oil and Gas Extraction
       211111: Crude Petroleum and Natural Gas Extraction
       211112: Natural Gas Liquid Extraction
  </code></pre>
  
  <p>And the CSV file:</p>
  
  <pre><code>,"11: Agriculture, Forestry, Fishing and Hunting",,
  ,,"111: Crop Production",
  ,,,"111110: Soybean Farming"
  ,,,"111120: Oilseed (except Soybean) Farming"
  ,,,"111130: Dry Pea and Bean Farming"
  ,,,"111140: Wheat Farming"
  ,,,"111150: Corn Farming"
  ,,,"111160: Rice Farming"
  ,,,"111191: Oilseed and Grain Combination Farming"
  ,,,"111199: All Other Grain Farming"
  ,,,"111211: Potato Farming"
  ,,,"111219: Other Vegetable (except Potato) and Melon Farming"
  ,,"112: Animal Production",
  ,,,"112111: Beef Cattle Ranching and Farming"
  ,,,"112112: Cattle Feedlots"
  ,,,"112120: Dairy Cattle and Milk Production"
  ,,,"112130: Dual-Purpose Cattle Ranching and Farming"
  ,,,"112210: Hog and Pig Farming"
  ,,"113: Forestry and Logging",
  ,,,"113110: Timber Tract Operations"
  ,,,"113210: Forest Nurseries and Gathering of Forest Products"
  ,,,"113310: Logging"
  ,,"114: Fishing, Hunting and Trapping",
  ,,,"114111: Finfish Fishing"
  ,,,"114112: Shellfish Fishing"
  ,,,"114119: Other Marine Fishing"
  ,,,"114210: Hunting and Trapping"
  ,,"115: Support Activities for Agriculture and Forestry", 
  ,"21: Mining",,
  ,,"211: Oil and Gas Extraction",
  ,,,"211111: Crude Petroleum and Natural Gas Extraction"
  ,,,"211112: Natural Gas Liquid Extraction"
  ,,"212: Mining (except Oil and Gas)",
  .
  .
  .
  ,"54: Professional, Scientific, and Technical Services",,
  ,,"541: Professional, Scientific, and Technical Services",
  ,,,"541110: Offices of Lawyers" 
  </code></pre>