## WeBWorK Problems

### Odd number of elements in anonymous hash

by Dave Rosoff -
Number of replies: 2

We use WeBWorK to generate randomized paper quizzes in some of our classes. This problem renders just fine on the web, but throws an error when we go to generate hardcopy. The error message is:

Odd number of elements in anonymous hash at line 75 of (eval 4063).

Problem source follows. You can see where I tried to fiddle with the variables hash on line 75 and where I tried using different Context or no Context. These did not help. Thanks in advance for any assistance.

##DESCRIPTION
## Absolute Value Equalites and Inequalities
##
##ENDDESCRIPTION
## DBsubject(Algebra)
## DBchapter(Absolute value expressions and functions)
## DBsection(Solving equations with absolute values)
## Institution(The College of Idaho)
## Author(RA Cruz)
## MLT(AbsVal3)
## Level(2)
## MO(1)
## TitleText1('Essentials of Intermediate Algebra')
## AuthorText1('Blitzer')
## EditionText1('1')
## Section1('.')
## Problem1('')
## KEYWORDS('inequalities','absolute value')
## Date: 2007/10

DOCUMENT(); # This should be the first executable line in the problem.

"PGstandard.pl",
"MathObjects.pl",
"contextInequalitiesAllowStrings.pl",
#"contextInequalities.pl", # this didn't work any better
# can't leave out the context
"PGchoicemacros.pl"
);

TEXT(beginproblem());

######################################
#  Setup

@letters = ("a", "b","g","p","q","t","w","x","y","z");
@pick = NchooseK(10,4);
$v0 =$letters[$pick[0]];$v1 = $letters[$pick[1]];
$v2 =$letters[$pick[2]]; #$v3 = $letters[$pick[3]]; # trying to fix a bug dwr 2023-10-11
$a = list_random(-10,-9,-8,-7,-6,-5,-4,4,5,6,7,8,9,10);$b = $a>0 ? random(-12,-2,1) : random(2,12,1);$RequiredFormat = "Enter your answer in the form: ".
"${BITALIC}x=m.$EITALIC $BR For more than one answer: ". "${BITALIC}x=m$EITALIC or${BITALIC}x=n$EITALIC ". "(include the word${BBOLD}or$EBOLD between the solutions), ". "or enter your answer as a set${BITALIC}{m, n}${EITALIC}.$BR".
"For no solution, type ${BBOLD}No solution${EBOLD} and ".
"if all real numbers are solutions, ".
"type ${BBOLD}All real numbers${EBOLD}.";

######################################
#  Main text

BEGIN_TEXT
Solve:
$PAR (a) $$|v0| = a$$$BR

$PAR (b) $$|v1| = b$$$BR
$BR END_TEXT ###################################### # Answer Context("Inequalities-AllowStrings"); Context()->variables->are($v0=>'Real',$v1=>'Real',$v2=>'Real');
#Context()->variables->are($v0=>'Real',$v1=>'Real',$v2=>'Real',$v3=>'Real');

sub errMsg {
my $ans = shift;$ans->{ans_message} =  $RequiredFormat if$ans->{ans_message} =~ /it looks like/;
return $ans; }$ans_a = ($a<0) ? Compute("No solution") : Compute("$v0=-$a or$v0=$a")->reduce; ANS($ans_a->cmp->withPostFilter(~~&errMsg));

$ans_b = ($b<0) ? Compute("No solution") : Compute("$v1=-$b or $v1=$b")->reduce;
ANS($ans_b->cmp->withPostFilter(~~&errMsg));$showPartialCorrectAnswers = 1;

###################################
# Solution

$display_a = ($a<0) ? "No solution" : "$$v0=-a$$ or $$v0=a$$";
$display_b = ($b<0) ? "No solution" : "$$v1=-b$$ or $$v1=b$$";

#Context()->texStrings;
BEGIN_SOLUTION
$PAR Solution:$BR $BR (a)$display_a
$BR (b)$display_b
END_SOLUTION
Context()->normalStrings;

ENDDOCUMENT();

### Re: Odd number of elements in anonymous hash

by Andrew Parker -
I could not replicate your "odd number of elements in hash" error. What version of WeBWorK/pg are you running?

I did, however, see other errors when attempting to compile the provided problem in LaTeX. These problems were resolved by wrapping your equations in LaTeX delimiters. I've made a few other changes, see below. The code provided below renders in HTML and generates hardcopy with no errors in WW 2.18.

Notes:
• The context you're using already has "R" for "all real numbers" -- none of the answers actually require this string, but you also didn't have a string in the context for this response.
• All three of the methods used to display the correct answers in the SOLUTIONS block have (almost) identical behavior. The fourth answer in the SOLUTIONS block is just an example of how the answer object can be extended to give more details about why it is the correct answer.
• There is actually no issue with extending the scope of the problem out to the original use of four unique variables in absolute value equations. I'm not sure what problems you were running into, but they were not related to the number of variables.

DOCUMENT(); # This should be the first executable line in the problem.

"PGstandard.pl",
"PGML.pl",
"contextInequalitiesAllowStrings.pl",
"PGchoicemacros.pl"
);

TEXT(beginproblem());

######################################
#  Setup

@letters = ("a", "b","g","p","q","t","w","x","y","z");
@pick = NchooseK(10,4);
$v0 =$letters[$pick[0]];$v1 = $letters[$pick[1]];
$v2 =$letters[$pick[2]];$v3 = $letters[$pick[3]];

$a = random(4,10)*random(-1,1,2);$b = $a>0 ? random(-12,-2,1) : random(2,12,1); do {$c = non_zero_random(-15,15);} until ($c !=$a && $c !=$b);
do {$d = non_zero_random(-15,15);} until ($d != $a &&$d != $b &&$d != $c);$RequiredFormat = "Enter your answer in the form: ".
"'$$x=m$$'.$BR For more than one answer:$BR".
"'$$x=m$$ or $$x=n$$' $BR". "(include the word${BBOLD}or$EBOLD between the solutions), ". "or enter your answer as a set $$\{m, n\}$$.$BR".
"For no solution, type ${BBOLD}No solution${EBOLD} and ".
"if all real numbers are solutions, ".
"type ${BBOLD}R${EBOLD}.";

######################################
#  Main text

BEGIN_PGML
Solve:

A) [|[$v0]|=[$a]]

A) [|[$v1]|=[$b]]

A) [|[$v2]|=[$c]]

A) [|[$v3]|=[$d]]

END_PGML

######################################

Context("Inequalities-AllowStrings");
Context()->variables->are(
$v0=>'Real',$v1=>'Real',
$v2=>'Real',$v3=>'Real'
);
None=>{},
"No solution"=>{None}
);

sub errMsg {
my $ans = shift;$ans->{ans_message} = $RequiredFormat if$ans->{ans_message} =~ /it looks like/;
return $ans; }$ans_a = ($a<0) ? Compute("No solution") : Compute("$v0=-$a or$v0=$a")->reduce; ANS($ans_a->cmp->withPostFilter(~~&errMsg));

$ans_b = ($b<0) ? Compute("No solution") : Compute("$v1=-$b or $v1=$b")->reduce;
ANS($ans_b->cmp->withPostFilter(~~&errMsg));$ans_c = ($c<0) ? Compute("No solution") : Compute("$v2=-$c or$v2=$c")->reduce; ANS($ans_c->cmp->withPostFilter(~~&errMsg));

$ans_d = ($d<0) ? Compute("No solution") : Compute("$v3=-$d or $v3=$d")->reduce;
ANS($ans_d->cmp->withPostFilter(~~&errMsg));$showPartialCorrectAnswers = 1;

###################################
# Solution

# the solution for part A is stored in a perl
# variable using $$...$$ delimiters for LaTeX
$display_a = ($a<0) ? "No solution" : " $$v0 = −a$$ or $$v0 = a$$";

# the solution for B uses [@ ... @] to execute perl
# inside of the PGML block, and the output of that
# perl code includes PGML LaTeX delimiters [...],
# so append "**" to tell PGML to parse the result

# the solution for C and D are simpler, using only
# the answer variables inside PGML LaTeX mode
BEGIN_PGML_SOLUTION
A) [$display_a] A) [@($b<0) ? "No solution"
: " [$v1 = -$b] or [$v1=$b]"@]**
A) [[$ans_c]] A) Because [$d] is [@ $d > 0 ? 'positive' : 'negative' @], the answer is [[$ans_d]]
END_PGML_SOLUTION

ENDDOCUMENT();