Any way to convert a string of text into a list of characters in the string?

The played code is text that varies. 
Instead of playing music:
	let played code be "[the topic understood]";
	let L be a list of unicode characters;
	add played code to L;

Here’s what I’m trying to do, but it’s not quite working. I’ve been wrestling with this for a while and feel like there’s probably a word I’m missing. Thanks!

I’m not sure exactly what you’re trying to do, because there’s something suspicious about your variables here. (The “played code” is being defined both as a global variable and as a temporary variable in the Instead rule. And L is being defined as a temporary variable, but nothing is being done with it.)

However, it’s clear you’re trying to convert a text to a list of unicode characters. As far as I know, Inform 7 has no built-in function to do that, and it looks like a rather un-Informy thing to want to do. The only thing Inform really wants to do with unicode characters is print them.

A more natural thing might be to convert your text into a list of texts, each containing a single character of the original. There may be a neater way of doing this that I don’t know about, but here’s something that works:

To decide which list of texts is the character sequence of (T - a text):
	let n be the number of characters in T;
	let L be a list of texts;
	repeat with j running from 1 to n:
		add "[character number j in T]" to L;
	decide on L.

With this, you can refer to (say) the character sequence of “[the topic understood]”. If the topic understood is “Imagine”, then this phrase will produce the list “I”, “m”, “a”, “g”, “i”, “n”, “e”.

If you really want unicode characters, I guess you could write a routine to convert single characters (the ones accepted by the parser, at least) into unicode via their character numbers. I don’t know a better method, but someone else might.

1 Like

I think I need to take a step back and think more about what I’m trying to do.

Thank you!

Just wanted to add something about this, since it’s a subtle issue that can produce extremely aggravating bugs.

The issue here is using the word “let” to change the name of a global variable. When you use “let” it creates a temporary variable–even if there’s already a global variable with the same name! As nicely demonstrated here.

The way to avoid this: Always change global variables by saying “Now the played code is…” rather than “Let the played code be…” Only use “let” for temporary variables. Probably it’s good practice to only use “let” when creating a new temporary variable, since you can change existing ones with “now.”

(§11.15 of Writing with Inform discusses Let and temporary variables, though I think it sort of suggests that you can’t use “let” with variable names that are already in use, which is not true–you can do this, it’ll just create this bug!)

1 Like