jointable -n vergleicht Schlüssel als Zahlen, Review Punkt 9 abgeschlossen

This commit is contained in:
wkh
2026-10-30 10:07:48 +01:00
parent 158ce5dee2
commit d6733e737e
5 changed files with 30 additions and 11 deletions
+8 -6
View File
@@ -96,7 +96,7 @@ end
Rdb.register('jointable', 'Join two tables into one where keys match',
'jointable [-a[1|2]] [-j[1|2] column]... [-n] [-] table1 [table2]') do |args|
all1 = all2 = false
all1 = all2 = numeric = false
k1 = k2 = nil
files = []
until args.empty?
@@ -108,7 +108,7 @@ Rdb.register('jointable', 'Join two tables into one where keys match',
when '-j' then k1 = k2 = args.shift
when '-j1' then k1 = args.shift
when '-j2' then k2 = args.shift
when '-n' then nil
when '-n' then numeric = true
else files << a
end
end
@@ -119,13 +119,15 @@ Rdb.register('jointable', 'Join two tables into one where keys match',
i1 = k1 ? t1.index_of(k1) : 0
i2 = k2 ? t2.index_of(k2) : 0
keep2 = (0...t2.head.size).to_a - [i2]
# -n compares keys as numbers: 01, 1 and " 1.0" are the same key
key = ->(v) { numeric && numeric?(v) ? Rdb.decimal(v) : v }
by_key = Hash.new { |h, k| h[k] = [] }
t2.rows.each { |r| by_key[r[i2]] << r }
t2.rows.each { |r| by_key[key.(r[i2])] << r }
used = {}
out = []
t1.rows.each do |r|
if (m = by_key[r[i1]]).any?
used[r[i1]] = true
if (m = by_key.fetch(key.(r[i1]), [])).any?
used[key.(r[i1])] = true
m.each { |r2| out << r + keep2.map { |i| r2[i] } }
elsif all1
out << r + [''] * keep2.size
@@ -133,7 +135,7 @@ Rdb.register('jointable', 'Join two tables into one where keys match',
end
if all2
t2.rows.each do |r2|
next if used[r2[i2]]
next if used[key.(r2[i2])]
blank = [''] * t1.head.size
blank[i1] = r2[i2]