WeBWorK Main Forum

Checking lists of strings and checking String objects

Re: Checking lists of strings and checking String objects

by Davide Cervone -
Number of replies: 0
Robin, you always have interesting problems. The problem, here, is subtle, and not something you would have been able to figure out on your own. It has to do with how the patterns are handled for matching against the various objects in the MathObject Context, like the strings, operators, and so on. most of these are folded into one large pattern, but there are a couple of special ones, like the pattern for a numeric constant. It turns out that the numeric constant pattern is handled before matching against the other objects, and since your strings start with numbers, the parser was matching the number first, and that left only the "AM" or "PM" as the rest. Since there was no definition for "AM" or "PM", WeBWorK complained about it.

There are several possible solutions to this. The first is to disable the numeric checking in the context by doing the following:

    Context()->{pattern}[number} = '^$';
This will make the number pattern be blank, which will never match anything (since a match it attempted only when there actually is something to match against). This should allow your time strings to be matched. Note, however, that by default your context also has definitions for all the operators (+, - , *, etc), functions (sin(), cos(), etc), constants (e, pi), and so on, so student could type these and get strange error messages. So you might want to remove them, as follows:
before adding your time strings to the context. This will make it impossible to type anything but time strings. However, you will still get some bad error messages (things like "Unexpected character '+'" if you type a plus). To solve that problem, you might use
    Context()->{error}{msg}{"Unexpected character '%s'"} =
         "Your answer doesn't look like a time value";
    Context()->{error}{msg}{"'%s' is not defined in this context"} =
         "Your answer doesn't look like a time value";
to translate those messages into a less precise, but perhaps less confusing, message.

Since you are using a List later on, you will want to allow the comma operator, so you can redefine it using

Finally, you might want to make aliases like "1AM"=>{alias=>"1 AM"} so that the space can be left out, since the space is required in the current form.

An alternative approach would be to declare a variable called "AM" and one called "PM", so that "1 AM" would really be "1*AM". You could adjust the output of the implicit multiplication using

    Context()->operators->set(" "=>{string=>" ",TeX=>"\ ",perl=>"*");
so that it would display properly in the preview and hardcopy. This would solve the problem of allowing spaces, and would allow mathematical operations in the hour (as in "5+3 am"), but it would not handle switching from AM to PM correctly, and you could do unusual things like "3 AM^2" or "AM 3" or just plain "AM" and not get an error message. You could also get error messages that suggest you should enter a formula (since the AM is a variable), and that might be confusing.

You suggest that what you want to do is set a new Unit for this. That is a nice idea, but I don't think that will quite work, because the way the unit library works by using multiplicative factors to convert the student's and professor's answers to a common unit, but the AM/PM difference is additive, not multiplicative.

There really needs to be a time-value context, so I'll put that on my list of things to work on .