## WeBWorK Main Forum

### Easiest way to have string answers with PGML? Need some examples.

by Christian Seberino -
Number of replies: 8
I know how to add strings to a Context object with PGML problems but I wonder if there is an easier way.

I saw some docs that mentioned String() but I couldn't find examples.

I'm hoping with some context I don't know about that the following will work or a close variant.....

BEGIN_PGML
What is capital of Texas? [____________]{String("Austin")}
END_PGML

### Re: Easiest way to have string answers with PGML? Need some examples.

by Paul Pearson -
Hi Christian,

I don't know about how to do this with PGML. There is a macro "contextArbitraryString.pl" and a context Context("ArbitraryString"); that may help you. In particular, it looks like this macro allows students to enter any string as an answer (so you don't have to add any strings to the context), but you will have to write a custom answer checker. The macro file is at

http://webwork.maa.org/viewvc/system/trunk/pg/macros/contextArbitraryString.pl?view=markup

Notice that there is a short example near the top of the macro file (in the part that is commented out by #'s). I'd also be interested to learn the answer to your original question (using PGML), so I hope that someone else has the answer.

Best regards,

Paul Pearson

### Re: Easiest way to have string answers with PGML? Need some examples.

by Christian Seberino -
Thanks. That worked exactly as expected in PGML.

I didn't need the String function after using the correct context you suggested.

I'm still confused what String("somestring") is used for.

cs

### Re: Easiest way to have string answers with PGML? Need some examples.

by Davide Cervone -
Can you post your final version of the problem, so others can see how you handled it?

As for String("something"), this is used to obtain an answer that is one of the pre-defined strings in your Context. For example, "NONE" or "DNE". You can add more strings to the Context, depending on what you want the student to be able to type. If the student types something that is not one of the predefined strings (and doesn't parse as a mathematical expression), the student will get a warning message indicating that.

There is a contextString.pl context that is useful if you have a list of possible answers that a student is allowed to type (like A, B, C, D, or POSITIVE, ZERO, NEGATIVE, or some such).

The contextArbitraryString.pl macro file defines a context in which a student can type anything without any error messages, leaving the responsibility to you to do all the parsing and error checking. For your problem, you will need to be careful to do things like strip leading and trailing spaces, and decide if you care about upper- and lower-case difference, and so forth.

In normal PG, if you want the answer be the word "NONE", you would use ANS(String("NONE")->cmp). In PGML, however, you don't really need to use String(), since using [______]{"NONE"} is sufficient.

### Re: Easiest way to have string answers with PGML? Need some examples.

by Christian Seberino -
Here is what I did below. It probably would have been more elegant to use multiple choice format instead.

DOCUMENT();
"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"PGcourse.pl",
"parserNumberWithUnits.pl",
"contextArbitraryString.pl",
);
TEXT(beginproblem());

Good luck!

Paul Pearson

### Re: Easiest way to have string answers with PGML? Need some examples.

by Christian Seberino -
Paul

Thanks for the reply. I agree a drop down menu or radio buttons are the way to go.

Do you have a PGML example or this?

Thanks!

cs

### Re: Easiest way to have string answers with PGML? Need some examples.

by Davide Cervone -
Here is an example that uses a popup menu. You have to create the PopUp() before the BEGIN_PGML/END_PGML block in order for it to work.
    DOCUMENT();

"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"parserPopUp.pl",
"PGcourse.pl",
);

TEXT(beginproblem());

$popup = PopUp(["increases","decreases","stays the same"],"increases"); BEGIN_PGML What happens to the pressure if the number of atoms is increased while keeping everything else the same? [____________]{$popup}
END_PGML

ENDDOCUMENT();

You can do something similar for radio buttons using parserRadioButtons.pl.

Davide

### Re: Easiest way to have string answers with PGML? Need some examples.

by Davide Cervone -
There are a couple of issues with this approach. For example, if the student types " increases" with a space before it, it will be marked incorrect. Same for "INCREASES", and so on for a variety of other capitalization issues. The ArbitraryString context is really meant when you want to process the student response without any interference from the MathObjects parser, and it really expects you to provide your own custom checker to do that.

You might be better off with the String context (available in contextString.pl". For this, you define the strings you want to allow, and then the student will get an error message indicating the allowed strings if he types anything else. That should take care of Paul's concern about leaving off a letter, for example.

Here is one version:

    DOCUMENT();

"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"contextString.pl",
"PGcourse.pl",
);

TEXT(beginproblem());

Context("String");
Context()->strings->are(
increases => {},
decreases => {},
"stays the same" => {},
);

BEGIN_PGML
What happens to the pressure if the number of atoms is increased while keeping
everything else the same?