column -d: Spalten löschen

This commit is contained in:
wkh
2026-10-12 00:10:36 +02:00
parent a9f3c0b255
commit a02e5cb109
7 changed files with 80 additions and 3 deletions
+13 -2
View File
@@ -11,9 +11,20 @@ Rdb.register('row', 'Make a new table where rows match logical condition',
end
Rdb.alias_for('select', 'row')
Rdb.register('column', 'Display columns of a table in any order', 'column [ Column ... ] < tableorlist') do |args|
Rdb.register('column', 'Display columns of a table in any order', 'column [ -d ] [ Column ... ] < tableorlist') do |args|
drop = args.first == '-d'
args.shift if drop
t = table_in
next t.write if args.empty?
next t.write if args.empty? && !drop
die 'usage: column -d Column ... < tableorlist' if drop && args.empty?
if drop # keep every column except the named ones
gone = args.map { |a| t.index_of(a) }
keep = t.head.each_index.reject { |i| gone.include?(i) }
die 'column -d would delete every column' if keep.empty?
next t.with(head: keep.map { |i| t.head[i] }, rows: t.rows.map { |r| keep.map { |i| r[i] } }).write
end
idx = args.map { |a| Rdb.column_index(t.head, a) }
head = args.each_with_index.map { |a, n| idx[n] ? t.head[idx[n]] : a }