From: Robert Klemme Date: 2011-10-13T21:15:30+09:00 Subject: Re: Reading CSV files into arrays and then comparing On Thu, Oct 13, 2011 at 1:49 PM, Khalid Rajput wrote: >   I am working on a small project which requires two 2-dimensional > arrays to be produced from CSV files and then compared on some values > and then producing a third array with as the result which then can be > stored as CSV file. > > Example : >   Name, qtymin, qtymax,qtycurrent, qtyorder > [["Atorva", 20, 50, 33,  10],["lipit", 10, ....]] > > 2nd file : >    Name,  qtyusedtoday > [["Atorva",  15],["lipit",4...]] > > The idea is that to read one row in the first file and then try to find > a match the same item in the second file by reading whole file. In case > of match/no match produce a row in third array which eventually could be > given out as a separate csv file. > > Thanks in advance For that Arrays are not well suited since the lookup is slow. Rather you would be doing something like this 1. read second file into a Hash using appropriate key (e.g. an Array of the key fields). 2. read the first file via CSV.foreach, for each record 2.1 do the lookup in the Hash from step 1 2.2 write out the resulting CSV line, either to stdout or to a file Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/