[I7] Varying Tables; Multiple Books You Can Consult Pages From

The title of this post is what I think my problem is (the need to provide tables that vary) while the remainder is the context for the problem (I need tables to hold page contents from books).

I’ve been looking at Example 303 (Pages) and it’s helpful to get started but it’s trickier when you might have multiple books to consult. I think I’ve generalized the example a bit, which I’ll show here, but I’m stuck when it comes to the table part.

For example, I have this:

A codex is a kind of thing.

A codex called the Brief History of Magic is carried by the player.
A codex called the prophecy is carried by the player.

Reading it in is an action applying to one number and one thing, requiring light.
Understand "read [number] in/from/of [something]" as reading it in.
Understand "read page [number] in/from/of [something]" as reading it in.

So I have two things that can be consulted by page number. Given that I now have to modify the example from 303 a bit as such:

Check reading it in:
	if the second noun is not the prophecy or the second noun is not Brief History of Magic:
		say "There are no pages in [the second noun]." instead.

Check reading it in:
	if the number understood is greater than the length of the second noun:
		say "There are only [length of the second noun in words] pages in the codex." instead;
	if the number understood is less than 1:
		say "You might just want to start on the first page." instead.

Carry out reading it in:
	read page number understood.

I think those parts are probably okay. Where I run into trouble is with the table part. I need to have two separate tables that are consulted. So I need that “read page number” part to be something like this:

To read page (N - a number):
	let the text source be the second noun;
	now the last page read of the text source is N;
	if the text source is Brief History of Magic:
		let the table source be the Table of Brief History Contents;
	if the text source is the prophecy:
		let the table source be the Table of Codex Contents;
	if there is a content corresponding to a page of N in the table source:
		choose row with a page of N in the table source;
		say "You read:[paragraph break][italic type]'[content entry]'[roman type][paragraph break]";
	otherwise:
		say "Page [N] appears to be blank."

This doesn’t work. I get the error:

You wrote ‘let the table source be the Table of Brief History Contents’ , but when a temporary value is created inside an ‘if …, …’ or an ‘otherwise …’, it only lasts until that line is complete - which means it can never be used for anything, because it goes away as soon as created. To make something more durable, create it before the ‘if’ or ‘otherwise’.

What I’m doing different from the example is with those let phrases in that I’m trying to provide a different table as part of the processing depending on what the second noun was.

For example:

read page 2 of the prophecy
read page 2 of Brief History of Magic

But I can’t follow the advice given in the error message because creating it before the if condition would make no sense. The whole point is for the table to reference to change based on a condition. I did try adding a line at the top of the rule as such:

let the table source be a table that varies;

But that also doesn’t work. The error is:

In the sentence ‘let the table source be a table that varies’ , I was expecting to read a value, but instead found some text that I couldn’t understand - ‘a table that varies’.

I’ve tortured this explanation to death to show my steps along the way in case I’m either (1) approach the problem poorly or (2) screwing up an obvious bit of implementation.

Trying to solve my own problem, I might have found something. I became aware of section 16.15, “varying which table to look at.” I did try something like:

let the table source be a table name that varies;

Note “table name” rather than just “table” as in my example from the first post.

So then my rule looks like this:

To read page (N - a number):
	let the table source be a table name that varies;
	let the text source be the second noun;
	now the last page read of the text source is N;
	if the text source is Brief History of Magic:
		now the table source is Table of Brief History Contents;
	if the text source is the prophecy:
		now the table source is the Table of Codex Contents;
	if there is a content corresponding to a page of N in the table source:
		choose row with a page of N in the table source;
		say "You read:[paragraph break][italic type]'[content entry]'[roman type][paragraph break]";
	otherwise:
		say "Page [N] appears to be blank."

Here notice that I use the “now the table source is” construct, as per that part of the manual. However, an error results:

To achieve ‘let the table source be a table name that varies’ , we’d need to be able to store a default value of the kind ‘a table names variable-pointer’, but there’s no natural choice for this.

So it looks like what I have to do is, outside of the rule, do this:

The table source is a table name that varies.
The table source is Table of Codex Contents.

Then the relevant portions of my rule would be:

	if the text source is Brief History of Magic:
		now the table source is Table of Brief History Contents;
	if the text source is the prophecy:
		now the table source is the Table of Codex Contents;

That seems to work!

But it pointed out that now neither of my codex sources are recognized as having pages.

Which means I generalized something wrong in the other code. And it turns out to be simple. Instead of this:

Check reading it in:
	if the second noun is not the prophecy or the second noun is not Brief History of Magic:
		say "There are no pages in [the second noun]." instead.

I need to have this:

Check reading it in:
	if the second noun is not a codex:
		say "There are no pages in [the second noun]." instead.

Hopefully this thread of me talking to myself is helpful to someone else who deals with something similar.

if the second noun is not the prophecy or the second noun is not Brief History of Magic:

This is almost correct. If you break down the logic in an extremely pedantic way, it turns out that you want “and” instead of “or”.

You could handle the first part (setting table source) in various other ways. For example, you could make a table whose values are table names. Or you could say “A codex has a table name called the contents.” Or you could keep doing what you’re doing, which works.

I’ll also note that table source doesn’t have to be a global variable. You could start your rule with:

To read page (N - a number):
	let the table source be a table name;
	[...]

The “…that varies” doesn’t work for local variables, which is a somewhat confusing quirk of the language, but it’s also not needed. The variable starts out with the default value the empty table.