title: Parsing CSV file. Separating into more files
body: <p>I am currently working on a small application that will allow someone to enter strings that'll separate a larger file into smaller files. </p>
  
  <p>I am having trouble getting the file to be split into the newer files. This is my code for my CSV method:</p>
  
  <pre><code>require 'csv'
  
  new_list = []
  old_list = []
  
  def import_csv(file)
    puts "Method begins"
    CSV.foreach("public_schools_list.csv", :encoding =&gt; 'windows-1251:utf-8', :headers =&gt; true) do |row|
      if row["company"].downcase.include?("academy" || "lutheran" || "jewish" || "private" || "christian")
        CSV.open("new_list.csv", "ab") do |n|
          n &lt;&lt; row
  
          puts "First Row"
  
          new_list &lt;&lt; n
        end
      else
        CSV.open("old_list.csv", "ab") do |o|
         o &lt;&lt; row
  
         puts "Second Row"
  
         old_list &lt;&lt; o
        end
      end
    end
  end
  
  puts "New Csv: #{new_list.count}"
  puts "Old Csv: #{old_list.count}"
  </code></pre>
  
  <p>I am just trying to check this code to see if it is splitting the files. I'm not sure if some of this is correct. There are currently only four items in the CSV list. I use the <code>count</code> method to check if they go into the correct files.</p>
  
  <p>What code am I missing to get the main file to be separated into two?</p>
  
  <p>here is my controller:</p>
  
  <pre><code>include CSVUpload
  
  def import
    csv_separate(params[:file].tempfile)
    redirect_to root_url
  end
  </code></pre>
  
  <p>then here is the module i am using:</p>
  
  <pre><code>require 'csv'
  module CSVUpload
  
      def csv_separate(file)
          new_list_counter = 0
          old_list_counter = 0
  
          # puts "Method begins"
          CSV.open("new_list.csv", "ab") do |new_list|
          CSV.open("old_list.csv", "ab") do |old_list|  
              CSV.foreach(file, :encoding =&gt; 'windows-1251:utf-8', :headers =&gt; true) do |row|
                if row["company"][/\b(?:academy|lutheran|jewish|private|christian)\b/i]
                  new_list &lt;&lt; row
  
                  new_list_counter += 1
                else
                  old_list &lt;&lt; row
  
                  old_list_counter += 1
                end
              end
            end
          end
        end
      end
  </code></pre>
  
  <p>and then here is the form:</p>
  
  <pre><code>&lt;div&gt;
    &lt;h3&gt;Import a CSV File&lt;/h3&gt;
     &lt;%= form_tag({action: "import"}, multipart: true) do %&gt;
     &lt;%= file_field_tag("file") %&gt;
     &lt;%= submit_tag "Import CSV" %&gt;
   &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>i hope that helps out a bit. Thanks!</p>