Skip to content

Commit

Permalink
Add counter to Merge method; cleanups in error messages for keys not …
Browse files Browse the repository at this point in the history
…present
  • Loading branch information
Ben Di committed Apr 4, 2012
1 parent e279015 commit 10c1c0a
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/suitecsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,24 @@ def initialize(filename, keys)
# Afterwards this CSV will be the combination of the two
# without duplicates, based on comparison of keys
def merge(other)

# Ensure the other CSV has the same headers
@headers.each do |header|
if not other.headers.index(header)
raise "ERROR: headers do not match, could not find: #{header}"
end
end

# Initialize variable to process rows processed & added
counter = added = 0

# Go through each row in the other CSV
other.each do |other_row|
$stdout.puts ">Line: " + other_row.to_s

counter += 1
# init the var to track whether this row is already present
already_present = false

# Go through each row in myself, see if the other's row is here
@matrix.each do |my_row|

$stdout.puts "\tTesting: " + my_row.to_s

# If the keys match this row is present - stop checking
if keys_match?(my_row, other_row)
already_present = true
Expand All @@ -138,12 +136,16 @@ def merge(other)

# Add this row to our table
if not already_present
$stdout.puts "Adding row: #{other_row}"
push_row other_row
else
$stdout.puts "Ignoring match: #{other_row}"
added += 1
end

#if counter % 500 == 0
# $stdout.puts counter
#end
end

return added
end

###########################
Expand Down Expand Up @@ -204,7 +206,7 @@ def join(other)

# Ensure the other CSV has the keys present
if not has_keys?(other)
raise "ERROR: Could not find key column #{key} in other CSV"
raise "ERROR: Could not find all key columns #{@keys.to_s} in other CSV"
end

# Because .length will change as we delete, we must save ahead of time
Expand All @@ -230,7 +232,7 @@ def join(other)
def unjoin(other)
# Ensure the other CSV has the keys present
if not has_keys?(other)
raise "ERROR: Could not find key column #{key} in other CSV"
raise "ERROR: Could not find all key columns #{@keys.to_s} in other CSV"
end

# Because .length will change as we delete, we must save ahead of time
Expand All @@ -256,7 +258,7 @@ def unjoin(other)
def bring(other, cols)
# Ensure the other CSV has the keys present
if not has_keys?(other)
raise "ERROR: Could not find key column #{key} in other CSV"
raise "ERROR: Could not find all key columns #{@keys.to_s} in other CSV"
end

cols.each do |col|
Expand All @@ -279,7 +281,6 @@ def bring(other, cols)
match_row = also_present?(row, other)

if match_row
$stdout.puts "Matched row: " + match_row.to_s
# Bring each column along for the ride
cols.each do |col|
row<< match_row[col]
Expand Down

0 comments on commit 10c1c0a

Please sign in to comment.