WeBWorK Problems

Using Format option in MultiAnswer

Using Format option in MultiAnswer

by Thomas Hagedorn -
Number of replies: 2
Is there an example of using the Format => option in the MultiAnswer function?

I would like to have a custom format to display the student's entered answers? I've tried using the following:

format => sub{
my ( $correct, $student, $self ) = @_;
my ($stuE1, $stuE2) = @{$student}[0..1];
my @stuC = @{$student}[2..3];
my @stuD = @{$student}[4..5];
return "yes";

but when Webwork prints it out, it displays

CODE(.......) instead of "yes" in the "Entered" box. Ultimately, I'd like to have the displayed string be along the lines of

( $stuE1, $studE2), ($stuC[0], $stuD[1])


In reply to Thomas Hagedorn

Re: Using Format option in MultiAnswer

by Thomas Hagedorn -
I think I figured it out. I used the following code:

format => "Eigenvalues = (%s, %s), c=(%s, %s), d=(%s, %s)" ,
tex_format => "\text{Eigenvalues }= (%s, %s), c=(%s, %s), d=(%s, %s)" ,

The format is supposed to be a string that is then used in a Perl sprintf command. The %s here will be filled in by the 6 answers that the student entered. The tex_format is the same thing, but in Tex format.

Here's two more questions:

1. Is it possible to omit one of the student's answers? Suppose I don't want to display answer number 2?

2. Is it possible to display the student's answers in a different order?
In reply to Thomas Hagedorn

Re: Using Format option in MultiAnswer

by Davide Cervone -
Yes to both. You can use the sprintf mechanism of selecting an entry in the list of answers by its index. That is, use "%n$s" where "n" is the index of the entry in the ist (the first one has n=1 not 0). So format=>'%3$s,%2$s,%1$s' would print the first three answers in reverse order. Note that you should use single quotes, not double quotes, to avoid having perl interpret this as variable substitution for $s. (If you use double quotes, you would need to use "%3~~$s,..." so as to get a single slash to quote the dollar sign.)

That also gives you a way to skip values. E.g., format=>'%s,%3$s,%s' would print items 1, 3 and 4 from the student's list.

Hope that helps.