I've searched and haven't found a method for this particular conundrum. I have two CSV files of data that sometimes relate to the same thing. Here's an example:

CSV1 (500 lines):

```
date,reference,amount,type
10/13/2015,,1510.40,sale
10/13/2015,,312.90,sale
10/14/2015,,928.50,sale
10/15/2015,,820.25,sale
10/12/2015,,702.70,credit

```

CSV2 (20000 lines):

```
reference,date,amount
243534985,10/13/2015,312.90
345893745,10/15/2015,820.25
086234523,10/14/2015,928.50
458235832,10/13/2015,1510.40

```

My goal is to match the date and amount from CSV2 with the date and amount in CSV1, and write the reference from CSV2 to the reference column in the corresponding row.

This is a simplified view, as CSV2 actually contains many many more columns - these are just the relevant ones, so ideally I'd like to refer to them by header name or maybe index somehow?

Here's what I've attempted, but I'm a bit stuck.

```
require 'csv'

data1 = {}
data2 = {}

CSV.foreach("data1.csv", :headers => true, :header_converters => :symbol, :converters => :all) do |row|
  data1[row.fields[0]] = Hash[row.headers[1..-1].zip(row.fields[1..-1])]
end

CSV.foreach("data2.csv", :headers => true, :header_converters => :symbol, :converters => :all) do |row|
  data2[row.fields[0]] = Hash[row.headers[1..-1].zip(row.fields[1..-1])]
end

data1.each do |data1_row|
    data2.each do |data2_row|
        if (data1_row['comparitive'] == data2_row['comparitive'])
            puts data1_row['identifier'] + data2_row['column_thats_important_and_wanted']
        end
    end
end

```

Result:

```
22:in `[]': no implicit conversion of String into Integer (TypeError)

```

I've also tried:

```
CSV.foreach('data2.csv') do |data2|
    CSV.foreach('data1.csv') do |data1|
        if (data1[3] == data2[4])
            data1[1] << data2[1]
            puts "Change made!"
        else
            puts "nothing changed."
        end
    end
end

```

This however did not match anything inside the if statement, so perhaps not the right approach?