record: 0 und 1 = erste Zeile, negative Nummern zählen vom Ende
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user