Extracting a number from a text

How can I convert contiguous digits in a text into an actual number?

As an illustration: in this code, how can I convert tt, a text composed of digits? Or is there some smarter, non-regex way I should be doing things like this.

lab is a room.

t is a text that varies.
t is "frob 99".
tt is a text that varies.

instead of jumping:
	if t matches the regular expression "(\d+)":
		now tt is text matching regular expression;
		say "[tt]";
		say line break;
		
test me with "jump".

output:

>jump
99

(This all may be very obvious, but I had a hard time getting a clean search).

I’m looking for natural language answers, since I’m actively working to learn using Inform in this way. Though I would count “it can’t be done with natural language” as a natural language answer.

Thanks in advance!

updated working code

t is a text that varies.
t is "frob 99".
tt is a text that varies.

instead of jumping:
	if t matches the regular expression "(\d+)":
		now tt is text matching regular expression;
		say the numerical equivalent of tt;
		say line break;
		
test me with "jump".

To decide what number is the numerical equivalent of (tt - text):
	let Z be the player's command;[this is a backup of the 'real' player's command]
	let N be -1;
	change the text of the player's command to tt;[now make the player's command our text]
	if the player's command matches "[number]":[magic]
		let N be the number understood;
	change the text of the player's command to Z;[change player's command back]
	decide on N.

I believe you can check if the text matches "[number]" and then get the value from the number understood. I need to look up the proper syntax, though. The vagaries of “matches” always trip me up.

1 Like

I’ve seen some of your posts about this, but I’m having a hard time applying them. I think because the example has to do with modifying the player’s command, I’m struggling to convert it to my purposes. I can get one of the examples you’ve posted to compile but retrofitting it for general crunching of text is something I haven’t worked out on my own.

Draconis code below

To decide what number is the numerical equivalent of (T - text):
	let Z be the player's command;
	let N be -1;
	change the text of the player's command to T;
	if the player's command matches "[number]":
		let N be the number understood;
	change the text of the player's command to Z;
	decide on N.

Oh, wait. Perhaps I misunderstand. Are you saying we’re not really doing anything with the player’s command? This is just a trick to craft our own “the number understood?”

Yeah, it’s a headache. “Matches” can compare a text against a text, or a text against a regular expression, or a snippet against a text, or a snippet against a topic…but not a text against a topic. "[number]" is a topic, so the only thing we can compare against it with “matches” is a snippet.

The rest of that mess is to turn T into a snippet so that we can do the comparison.

2 Likes

I think you’re thinking about it right, but I wouldn’t say we’re not really doing anything with the player’s command. We’re backing it up, then overwriting it in order to use the if the player's command matches "[number]" phrase that assigns number understood, then we’re restoring the previous value of the player’s command so this routine doesn’t screw up other stuff.

2 Likes

OK, I have this working in my project. Thank you!

I wonder if it would be useful to have a phrase like…

with (T - text) converted to snippet (S - snippet):
    [do something with S here]

Like Python with-statements.

EDIT: Actually, no, I’m overthinking again. The only reason to do this is to compare it against a topic, so just embed that in a phrase and be done with it.

I’ve been following this along. Very cool to see a difficult problem like this solved collaboratively, and I’m sure it will be useful in the future.

1 Like