Skip to content

PG uniq does not preserve order #1275

@dlglin

Description

@dlglin

The uniq function in PGbasicmacros.pl returns an array of unique items from the input array, but it doesn't maintain a consistent order of the items in the returned array.

Consider the following MWE

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

## Do NOT show partial correct answers
$showPartialCorrectAnswers = 0;

@answers = ('a','b','c','b','a');
@options = uniq(@answers);

$mc = RadioButtons([@options],'b');

BEGIN_PGML
[_]{$mc}
END_PGML

ENDDOCUMENT();

Each time you load the problem the options can be presented in a different order, which means that even if you select the correct answer it may be marked incorrect since the array could be reordered on submit.

All of the examples that I found in the OPL (Contrib actually) take this into account and sort the resulting array, so it may be that this behaviour is understood, but there is a comment in the code that says "sort is causing trouble with Safe.??", which makes me think that the intention was to have the function sort the array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions