Snoopy Dog wrote:
> OK, I think I have figured out most of it...  Amazing how the
> documentation can help out when you see the code in action.
>
> mergedata = [data[0]] - creates a new array with just one element in it!
>
> the mergedata.push - pushes elements on to the array (appends).

You got it. It could also be written:

mergedata = Array.new(data.at(0))
...
mergedata << x2

(All of these are synonyms for the way it was written, which is why I
mention them.)

> and the data.each_cons(2) - I ASSUME that this takes two elements at a
> time from the data array.

Kind of, sort of...see the docs:
http://ruby-doc.org/core/classes/Enumerable.html#M002115


As to the problem, you could also do something like this, which is a
little more verbose than the other solutions, but is (I think) easier
to understand:

data1 = File.readlines(file1) # historical
data2 = File.readlines(file2) # new

# dates are from index 5-12 in the row string
# like your example data, change as needed
dates1 = data1.collect { |row| row[5..12] }
dates2 = data2.collect { |row| row[5..12] }

i = 0
while i < dates2.size
  if dates1.include?(dates2[i])
    dates2.delete_at(i)
    data2.delete_at(i)
  else
    i += 1
  end
end

out_data = (data1 + data2).sort


Also, you could use a little trick with Hash; just index the rows in a
hash by their date, then when you hit a duplicate date, you'll just
overwrite the previous value indexed by that date (change the order of
reading in file1 and file2 to keep historical rows rather than new
ones, the current order keeps new rows):

hash = {}
data = File.readlines(file1) +
       File.readlines(file2)
data.each { |row|
  date = row[5..12]
  hash[date] = row
}
data = hash.values.sort

Regards,
Jordan