Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, there's a slight bug with your code. Instead of:</p> <pre><code>csv &lt;&lt; people[0] </code></pre> <p>You would need to do the following if you don't want to change your loop code:</p> <pre><code>csv &lt;&lt; people.shift </code></pre> <p>Now, the following solution will add only the first occurrence of a person, discarding any subsequent duplicates as determined by id (as I am assuming ids are unique). </p> <pre><code>require 'csv' puts "loading data" people = CSV.read('input-file.csv') ids = [] # or you could use a Set CSV.open("output-file", "wb") do |csv| #write the first row (header) to the output file csv &lt;&lt; people.shift people.each do |p| # If the id of the current records is in the ids array, we've already seen # this person next if ids.include?(p[0]) # Now add the new id to the front of the ids array since the example you gave # the duplicate records directly follow the original, this will be slightly # faster than if we added the array to the end, but above we still check the # entire array to be safe ids.unshift p[0] csv &lt;&lt; p end end </code></pre> <p>Note that there is a more performant solution if your duplicate records <em>always</em> directly follow the original, you would only need to keep the last original id and check the current record's id rather than inclusion in an entire array. The difference may be negligible if your input file doesn't contain many records.</p> <p>That would look like this:</p> <pre><code>require 'csv' puts "loading data" people = CSV.read('input-file.csv') previous_id = nil CSV.open("output-file", "wb") do |csv| #write the first row (header) to the output file csv &lt;&lt; people.shift people.each do |p| next if p[0] == previous_id previous_id = p[0] csv &lt;&lt; p end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload