record: 0 und 1 = erste Zeile, negative Nummern zählen vom Ende

This commit is contained in:
wkh
2026-10-11 22:51:43 +02:00
parent d5286c3600
commit 777a3b56e2
4 changed files with 30 additions and 11 deletions
+4 -2
View File
@@ -72,10 +72,12 @@ end
Rdb.register('record', 'Find and output a record from a table', 'record number < table') do |args|
die 'usage: record number < table' unless args.size == 1
die "'#{args[0]}' is not a record number (1, 2, 3 ...)" unless args[0].match?(/\A\d+\z/) && args[0].to_i.positive?
die "'#{args[0]}' is not a record number (1, 2, 3 ... or -1 for the last)" unless args[0].match?(/\A[+-]?\d+\z/)
# 0 and 1 are the first record; -1 is the last, -2 the one before it ...
t = table_in
row = t.rows[args[0].to_i - 1]
n = args[0].to_i
row = n.negative? ? (t.rows[n] if -n <= t.rows.size) : t.rows[[n, 1].max - 1]
t.with(rows: row ? [row] : []).write
end