On Mon, Feb 26, 2007 at 10:50:22PM +0900, Rafael G. wrote:
Hi guys, im a newbie in Ruby i have to parse two CSV files to compare
2 columns of the given files. My problem is that i tried a lot of
different methods to handle this, i tried to put the entire column in
an array and the other one two then test for the bigger array to make
a loop thought it and compare both files like that. It did not work
Well, posting your code might allow someone to help you spot what’s
I’d suggest first you check that the two arrays are being read in
if they are called a1 and a2, then “puts a1.inspect” and “puts
will print them to the screen. Then you know whether the problem is in
reading them, or in comparing them.
Posting a more precise description of what you’re trying to do, along
some sample data and what output you expect, would also make it easier
someone to help you.
PS: I was told to make this tool in Java but, AFAIK Ruby is better for
handling file text.
The better language is the one which you can actually use to get the job
How you do this in Ruby depends on what exactly you mean by ‘compare’,
you didn’t define exactly what you’re trying to do. I’m guessing you
check for values which are in the first file but not in the second, or
versa. For a simple solution, have a look at Array#include?
For a more efficient solution, you could first sort the two arrays and
walk down them with two pointers i and j. When a1[i] == a2[j] then you
increment both i and j. When a1[i] < a2[j] then you know an item is
in a2, and just increment i. When a1[i] > a2[j] then you know an item is
missing in a1, and just increment j.
Incidentally, you don’t even need Ruby to do this; then shell command
can do this for you (as long as you use ‘sort’ to pre-sort your input)