How Do I Determine the Datatype?

According to the “Fundamental Datatypes” page of the System Manual, “This lets you determine a given value’s type at any time…” But I don’t know where to find the code that one would use for this determination.

I need to determine whether a given chunk of stuff is an Integer, or whether it’s something else. My code uses this:

local words = str.split(' '); local x = new BigNumber(words[2]);
But it’s quite possible that the string being passed to the BigNumber constructor is not something like ‘37’ but perhaps something like ‘monkeys’. In the latter case, it’s at least possible that the value of x won’t be of the Integer type. If it is, I’m in trouble, because I need to trap the case where the string doesn’t contain a series of digits.

You want to look at this page, tads.org/t3doc/doc/sysman/reflect.htm

Note that in your example code, ‘x’ is always going to be a BigNumber, because under no circumstance would:

new BigNumber(...)

return anything else than a BigNumber. It is never going to be a string. If the source string is “monkey”, x will be a BigNumber with a value of 0.

That’s what I was afraid of. So I guess the answer is, in order to determine whether a string contains only digits, I need to use a regular expression … a tactic I have managed to avoid up to now. I’m sure it’s not THAT hard (I hope).

Don’t see why. In the use case under discussion, I understood the problem to be, which page does the user want to read?

If the page number entered by the player equates to 0, why isn’t it a valid answer to say, “The book does not contain a page 0.”

It’s a good idea to parse and validate the string anyway, otherwise you would leak the internal workings of Tads syntax to the player. For example, the player would be able to enter something like “5.15e-10” and your game would accept it as valid input.

When you need to pass player input to internal functions that are not intended directly for the player, always validate the input.

You’d have to see the code for this to be clear. I want to be able to parse a wide variety of inputs, and produce a sensible response. If the player types ‘read page monkeys’, I don’t want the output to be, “The book does not contain a page 0.”

Your game, your rules.

If it were me, I’d be happy with something like…

if(pageNum == 0)
    "That's not a valid page for this book.";
else if(pageNum > numPages)
    "There aren't that many pages in the book.";

…rather than subjecting myself to regex contortions.

That does two things—tells the user the page is not valid and, more importantly, that the input was not correctly entered on the command line.

But, as noted, YMMV, or, as Mark Twain put it so eloquently, “That’s what makes horse races.”

Jerry

Or just (pseudocode):

local pageNum = new BigNumber(words[2]);
if( pageNum == 0 && words[2] == '0' ) 
     "The book does not contain a page 0.";
else if( pageNum == 0 )
    words[2] + " is not a number.";
else if( pageNum > numPages )
    "There aren't that many pages in the book.";