Skip to content

Instantly share code, notes, and snippets.

@hojberg
Created July 28, 2009 11:43
Show Gist options
  • Save hojberg/157168 to your computer and use it in GitHub Desktop.
Save hojberg/157168 to your computer and use it in GitHub Desktop.
# Delete duplicates
rows = []
res.each do |row|
# skip if already saved in new array
next if rows.detect { |r| r[3] == row[3] }
# Find multiple instances sale
rows_with_same_sale_id = res.select { |r| r[3] == row[3] }
# create an array with only the markers ie. ["I", "N", "E"]
same_sale_id_markers = rows_with_same_sale_id.map(&:first)
# Add I or N using I < N
if same_sale_id_markers.include?("I") && !same_sale_id_markers.include?("N")
rows << rows_with_same_sale_id.detect { |r| r.first == "I" }
elsif same_sale_id_markers.include?("N")
rows << rows_with_same_sale_id.detect { |r| r.first == "N" }
end
# Add E, D or C using E < (D,C)
if same_sale_id_markers.include?("E") && (!same_sale_id_markers.include?("D") || !same_sale_id_markers.include?("C"))
rows << rows_with_same_sale_id.detect { |r| r.first == "E" }
elsif same_sale_id_markers.include?("D") || same_sale_id_markers.include?("C")
rows << rows_with_same_sale_id.detect { |r| r.first == "D" } if same_sale_id_markers.include?("D")
rows << rows_with_same_sale_id.detect { |r| r.first == "C" } if same_sale_id_markers.include?("C")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment