Calling a phrase with (TR - a table row)


I was wondering whether I can create a phrase that uses as variables not only a table name, but also a specific row of that table:

To check row (R - a table row) of table (T - a table name): ...
I know table name exists as a possible variable, but I haven’t seen the table row option around. If it’s possible, what is the right syntax?

(The reason is that I have several rules that lead to the same series of "if/else: say … " results, but a table is also included. Instead of copying/pasting the same conditions and results, I am hoping for a phrase that can do it.)


The easiest way is just to pass it as a number, then choose the row by that number (“choose row number N in T”).

(Forehead slapping!) But of course!

Thanks, Daniel!

(For future reference, there’s also a type of value called “table column”, which is a named column from a table. The Standard Rules is the main place you’ll find that, in the definitions of the sorting phrases.)

So, there is a “table column” but not a “table row” type of value?

OK, I tried to work it out myself, but I failed. It is more confusing than I thought.

After asking Colin about a topic listed in the Table of Topics:
	[how can I choose a row and name it as R?*]
	run Colin's Reply Routine for row R of the Table of Topics.

To run Colin's Reply Routine for row (N - a number) of the (T - a table name):
  • So my question is how do I phrase a “let R be the number of the row that lists the topic…” kind of instruction?

So, Inform doesn’t let you do this by default. It requires a little bit of I6 hacking.

To decide which number is the/-- current row index: (- ({-my:ct_1}) -).

Now you can do this:

choose row 1 in the Table of Foobar; say the current row index; say line break; choose row 2 in the Table of Foobar; say the current row index.

Basically, {-my:SOMETHING} accesses an I6 local variable, declaring it if it doesn’t exist. ct_1 is the local variable used by all the table routines to hold the index of the current row. (Similarly, ct_0 is a pointer to the current table.) Whenever you “choose” a row, whether explicitly or by looking up a topic, it sets ct_1 to that index.

(Tested in 6L38; this relies on an undocumented method of accessing local variables, which could disappear and break without notice. Sorry about that; there isn’t really a better way to do it.)

One thing you could do is make another column in the table with a unique ID for each row, and pass that ID to your routine. If you’re not going to be rearranging the table in the course of play you could just make an “Index” column numbered from 1 to n and then pass that number.

Yes, Matt, I can definitely do that!

(My I6 hacking days are yet to come, Daniel! I do appreciate the help, though!)

So, what I’m asking is not common? I thought that it is something that several users would need at some point.

Not really, no. Usually once you’ve selected a table row you do whatever you need to do within the same rule, or get the relevant values out of the table and pass those into other phrases.

(By the way, no I6 is necessary if you want to use those phrases, just copy-paste my first line of code into your story. Then you should be able to access “the current row index” at any point.)