(I split a few posts from Issue with $text widget, and calculations from its results, inside a procedure, as they might be worth discussing on their own.)
I wonder if it would make more sense to store the table in a tiddler and write a quick procedure to extract values from that table based on row- and column-headers. The idea is to have a tiddler, say skills
, with content like this:
# Skill-cost table
* E A H VH
-3 - - - 1
-2 - - 1 2
-1 - 1 2 4
+0 1 2 4 8
+1 2 4 8 12
+2 4 8 12 16
+3 8 12 16 20
+4 12 16 20 24
+5 16 20 24 28
and a macro/procedure that works like this:
Wikitext | Result |
---|---|
<<table-lookup skills +2 H>> |
12 |
<$macrocall $name="table-lookup" tiddler="skills" row="+4" col="VH"/> |
24 |
<$transclude $variable="table-lookup" tiddler="skills" row="-1" col="A"/> |
1 |
You wouldn’t have to pass plain text; any variable or field references should also work for the latter two syntaxes.
I created a tiddler to do this. It’s JS, though, not wikitext. I’m sure we could redo it in wikitext, but JS is still much quicker for me. The important code looks like this:
exports.run = function(tiddler, row, col) {
const text = ($tw.wiki.getTiddler(tiddler) || {fields: {text: ''}}).fields.text;
const lines = text.split(/\r?\n/).filter(Boolean).filter(r => !r.startsWith('#'))
const headers = (lines.length ? lines : [''])[0].split(/\s+/)
const records = lines.slice(1).map(s => s.split(/\s+/))
const x = headers.indexOf(col)
const y = records.findIndex(r => r[0] == row)
return (x > -1 && y > -1) ? records[y][x] : null
};
This does not handle the Extra
row of your table – I wasn’t really sure if that was part of the main lookup table or something additional.
I feel like this technique would make many such lookups easier to deal with, both quicker to write and easier to maintain.
This is mostly a proof-of-concept. There are a number of weaknesses:
- There’s no way for any of the keys to include spaces. That could definitely be fixed, using TW’s double-square brackets, but it would add some implementation complexity.
- It requires that initial
*
as a placeholder on the first row. - It does not signal a missing value or even a missing tiddler, only returning a blank.
- It would not allow you to use
#
as a row header (as it’s used to indicate a comment.)
But before spending any time on cleaning up such things, it’s necessary to decide whether this is a useful idea at all. I’d love to hear feedback from the OP and others.
You can try this by downloading this link, dragging it to any wiki, saving and reloading (because it’s a JS module) and then opening the Skills Demo
tiddler.
table-lookup.json (2.3 KB)