From: Axel Etzold Date: 2008-05-13T04:26:51+09:00 Subject: Re: array diff -------- Original-Nachricht -------- > Datum: Tue, 13 May 2008 03:52:21 +0900 > Von: James Dechiaro > An: ruby-talk@ruby-lang.org > Betreff: array diff > Hello > > I am new to ruby and trying to do a diff on two csv files. > I am putting each row into an array and then subtracting the arrays into > a new array, then taking that array and printing out the records. > > The problem I am running into is I would like the badRecords method to > return the actual record lines that are not present in csv2.csv but > instead it is returning all records in csv1.csv. The other problem I see > is the code is running rather slow, cpu usage spikes up to 99% when > running. Any insight on improvements would be appreciated. > > Thanks! > > > #!/usr/bin/env ruby -wKU > > require 'rubygems' > require 'faster_csv' > > def Array1 > > getNumber = FCSV.open("csv1.csv") > getNumber.collect do |row| > return row[1] > end > end > > def Array2 > > getNumber = FCSV.open("csv2.csv") > getNumber.collect do |row| if (row[5].include?("Originating")) && > (row[41].include?("y")) > return row[20] > end > end > end > > def SumArray > > SumArray = Array1 - Array2 > if SumArray.empty? > puts "records have been validated" > Process.exit!(0) > else > return SumArray > end > end > > def badRecords > > my_file = File.open('badRecords.csv','w') > records = FCSV.open("csv1.csv") > records.collect do |row| row[1].eql?(SumArray) > my_file.puts row.inspect.gsub(/\[|[a-z]*\]$/, "") > end > my_file.close > end > end > badRecords > -- > Posted via http://www.ruby-forum.com/. James, welcome to Ruby! You'll like it :) You could do something like this: csv_1_array=IO.readlines("csv1.txt") csv_2_array=IO.readlines("csv2.txt") result_array=csv_2-csv1 # (removes doublets, triplets etc also.) Best regards, Axel -- GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen! Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx