## WeBWorK Main Forum

### parserRadioButtons.pl - order (last, first) and checked.

by William Boshuck -
Number of replies: 3
I noticed some curious (to me) behaviour of
the objects created by parserRadioButtons.pl:
if I specify order then I get spanked for specifying
checked - it looks like I get spanked by the following
clause (in orderedChoices)

Value::Error("Item $i of the 'order' option was already specified.") if not exists$remainingChoices{$label}; because$label is really "%$label" at this point. (I can produce the same behaviour if checked is among either first or last.) I can work around this by changing the code in the problem, or I can work around it by replacing the foregoing clause by Value::Error("Item$i of the 'last' option was already specified.")
if not (exists $remainingChoices{$label} or exists remainingChoices{"%$label"});$label = (exists $remainingChoices{"%$label"} ? "%$label" :$label);

(which I'd need to do in three places; one for each of order,
first and last).

Below is a little file that illustrates the behaviour. Is this intended
behaviour?

cheers,
-wb

DOCUMENT();

'PGstandard.pl',
'MathObjects.pl',
);

TEXT(beginproblem());

$radio = RadioButtons([ 'This is the first choice.', 'This is the second choice.', 'This is the third choice.', 'This is the fourth choice.', 'This is the fifth choice.'], 4, labels => [1, 2, 3, 4, 5], #labels => "123", order => [1, 2, 3, 4, 5], #order => [0, 1, 2, 3, 4], checked => '1' ); BEGIN_TEXT Select one of the following. (Just click on 'Submit Answers' if your choice is already selected.)$PAR
\{ $radio->buttons \} END_TEXT ANS($radio->cmp);

ENDDOCUMENT;
In reply to William Boshuck

### Re: parserRadioButtons.pl - order (last, first) and checked.

by Davide Cervone -
Thanks for the error report. You are correct that the percent signs in the keys of the choiceHash (which get copied to the remainingChoices hash) are the problem. I think the solution is really not to put the percent signs on the keys until the end of the orderedChoices routine, since they are needed only for the ans_radio_buttons() macro. That simplifies the code, and puts the percents in only when they are really needed.

I have modified the version in the CVS repository. See if that doesn't take care of things for you.

Davide

PS, the order => [0,1,2,3,4] option still produces an error message about the first item being used twice. That's because the values in the order array can be either indices into the answer array or labels from the labels array (with the later taking precedence). In this case, the labels are also numbers, so there is an ambiguity. Since 0 is not one of the labels, it references the 0th (i.e., first) answer in the array; but since 1 is a label, that is used as a label reference rather than an index, and the label '1' also refers to the first answer. So both 0 and 1 reference the same answer. There is no easy way around this but to use the (numeric) labels throughout.
In reply to Davide Cervone

### Re: parserRadioButtons.pl - order (last, first) and checked.

by William Boshuck -
Yes, that part (combining order, first or last and checked)
works fine (and I am a little relieved to see the demise of