WeBWorK Main Forum

perl keys function constantly re-evaluated?

perl keys function constantly re-evaluated?

by Andrew Dabrowski -
Number of replies: 2
Here's a minimal example that shows a problem with perl's keys function within WW. When you open the following problem you will find that every time you hit "Check Answers" the elements in the list @vertKeys change order.

This has an easy workaround: add lex_sort to the definition of @vertKeys.

I take it this happens because even though the seed for the problem remains constant, the action of the keys function depends on other random factors that can't be controlled. But this behavior is counterintuitive; maybe the workaround should be incorpated into the WW base to produce a PGkeys command?
DOCUMENT();

loadMacros("PGstandard.pl",
"PGML.pl",
);

TEXT(&beginproblem);

%vertices = (
I => [10,30], J => [20,30], K => [30,30], L => [40,30],
E => [10,20], F => [20,20], G => [30,20], H => [40,20],
A => [10,10], B => [20,10], C => [30,10], D => [40,10] );
@vertKeys = keys( %vertices );



BEGIN_PGML

[@ join( ",", @vertKeys ) @]

END_PGML


ENDDOCUMENT();

In reply to Andrew Dabrowski

Re: perl keys function constantly re-evaluated?

by Davide Cervone -
I'm not sure if this is meant as a complaint or just a point of information for others. But what you describe is the correct behavior. Note first that a hash (like %vertices is an unordered collection, and keys() is not required to return the keys in any particular order. Indeed, it turns out that it is intentionally returned in random order (and that random order will vary each time the code is run). See the keys function perl documentation for more details, and in particular the link to the Algorithmic Complexity Attacks for further explanation.
In reply to Davide Cervone

Re: perl keys function constantly re-evaluated?

by Andrew Dabrowski -
" I'm not sure if this is meant as a complaint or just a point of information for others."

The latter. The behavior is understandable (indeed, I explained it myself) but counterintuitive from the point of view of a humble problem author: why should hitting "Check Answers" cause all the perl code to be re-evaluated?

I hope I prevent someone else from suffering as I did.