Guest User

Untitled

a guest
Feb 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #!/usr/bin/ruby -w
  2.  
  3. require 'csv'
  4.  
  5. old_table = CSV.open('file.csv','r')
  6.  
  7. index = [{}, {}, {}, {}, {}] # create one index hash for each column
  8. multi_key_index = {}
  9.  
  10. old_table.each do |row|
  11. row.each_with_index do |column,col_num|
  12. (index[col_num][column] ||= []) << row
  13. end
  14. (multi_key_index[row[1] + row[2]] ||= []) << row
  15. end
  16.  
  17.  
  18. ================
  19. input format for CSV
  20. * only three options for column 0
  21. * each column 0 has two other corresponding rows, in this case the last 3 rows make up a set that needs to be extracted and reformatted.
  22. * every line comes in a set of 3 (although only one set is show here)
  23. * need to find every corresponding orange and banana row for every apple row that is found
  24.  
  25. "apple","John","qwerty",4,7
  26. "banana","Jane","qwerty",2,7
  27. "apple","Rachel","zxcv",8,3
  28. "apple","Bob","asdf",5,4
  29. "orange","Bob","asdf",4,6
  30. "banana","Bob","asdf",5,9
  31.  
  32. ================
  33. output format for CSV -- need to
  34.  
  35. Columns correspond to "apple","banana","orange",name,asdf (the numbers are from column[3])
  36.  
  37. 5,2,4,"Bob","asdf"
Add Comment
Please, Sign In to add comment