Reading CSV files into arrays and then comparing

Hi,
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

On Thu, Oct 13, 2011 at 1:49 PM, Khalid R. [email protected]
wrote:

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

Robert K. wrote in post #1026417:

On Thu, Oct 13, 2011 at 1:49 PM, Khalid R. [email protected]
wrote:

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

Thanks robert,
I have recently started working in ruby and am a noobie. I had only
read that ruby community is very helping in books but i really finds it
true. I’ll try this approach and will reply how it goes

Thanks once again

Hi again…
I am having some trouble in making a hash using appropriate keys as
both csv files have no headers. Only i know what column represents what
according to its position. The files are not that big so may be arrays
approach albeit slow may work alright for me . The firsr file i mention
is basically the record of inventory and the second file contains name
and quantity used on a single day. The purpose is to generate a third
csv file which then can be fed into system which contains all names and
quantity needs to be ordered for items (from file 1) for which value of
qtymin has reached after substracting todays consumption. Regards

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs