Use Perl Array Variables

Perl arrays let you use tabular data or iterate over a list of values, for example, a set of data points for a question. In addition, the Question Editor supports a special array called @dat.

How-ToInstructor HelpWebAssign

Perl array variables are prefixed by the at sign (@). In addition, all Perl variable names must follow these rules:

  • Variable names must contain only letters (a-z, A-Z), underscores (_), and numeric digits (0-9).
  • The first character of a variable name must be a letter (a-z, A-Z) or underscore (_).
  • Variable names are case-sensitive, so myvariable is not the same as MyVariable.

Some variable names are used by WebAssign. These variables are listed in the documentation.

One-Dimensional Arrays

One-dimensional arrays are like grocery lists. For example:

wine cheese bread

In Perl, you enclose the list in parentheses and separate individual list items with commas. For example:

@groceries = ('wine', 'cheese', 'bread');

To refer to a single element in the array, specify the array variable as a scalar with an index indicating which element you are referring to. The index starts with 0 and follows the variable name in brackets. For example:

The second item on my grocery list is <eqn $groceries[1]>.

You can mix numeric and text data in an array.

Two-Dimensional Arrays

Two-dimensional arrays let you work with tabular data. For example:

Mercury

0.39

Venus

0.72

Earth

1

Mars

1.52

Jupiter

5.20

Saturn

9.54

Uranus

19.18

Neptune

30.06

In Perl, you enclose the entire table in parentheses, enclose each row in brackets, and separate both rows and table cells with commas. For example:

@dat = ( ['Mercury' , 0.39 ],
         ['Venus'   , 0.72 ],
         ['Earth'   , 1    ], 
         ['Mars'    , 1.52 ],
         ['Jupiter' , 5.20 ],
         ['Saturn'  , 9.54 ],
         ['Uranus'  , 19.18],
         ['Neptune' , 30.06] );

To refer to an element in a two-dimensional array, specify the array variable as a scalar with two indexes indicating the row and column of the element you are referring to. The index values start with 0 and follow the variable name in brackets. For example:

The average distance from the Sun to <eqn $dat[4][0]> is <eqn $dat[4][1]> AU.

The @dat Array

WebAssign provides support for viewing one- and two-dimensional array values in the Question Previewer when you use an array named @dat. If your question contains the @dat array, the Question Previewer displays an additional tab named Array, which displays the contents of the array in addition to the question code and a preview of your question.

image of Array tab in the Question Previewer

In all other respects, the @dat array is a normal array and can contain whatever values you want.

  1. In an <EQN> or <eqn> tag, set the values for elements of the array using assignment statements like the following examples.

    @planets = ('Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune');
    @mean_au = (0.39, 0.72, 1, 1.52, 5.20, 9.54, 19.18, 30.06);
    @dat = ( ['Mercury' , 0.39 ],
             ['Venus'   , 0.72 ],
             ['Earth'   , 1    ], 
             ['Mars'    , 1.52 ],
             ['Jupiter' , 5.20 ],
             ['Saturn'  , 9.54 ],
             ['Uranus'  , 19.18],
             ['Neptune' , 30.06] );
    $dwarf_planets[0] = 'Pluto';
    $dwarf_planets[1] = 'Eris';
  2. In an <EQN> or <eqn> tag, reference specific array element values by specifying the array variable as a scalar with an index indicating which element you are referring to, as in the following examples.

    $thisplanet = $planets[7];         # sets $thisplanet = Neptune
    $thismean_au = $mean_au[7];        # sets $thismean_au = 30.06
    $firstplanet = $dat[0][0];         # sets $firstplanet = Mercury
    $firstorbit = $dat[0][1];          # sets $firstorbit = 0.39

Example Question Using Array to Define Possible Answers The following table summarizes an actual question. QID Name Mode Fill-in-the-Blank Question <eqn> @dat= ('John Lennon', 'Paul McCartney', 'George Harrison', 'Ringo Starr'); '' </eqn> Name one of the Beatles: <_> Answer <EQN join("\t",@dat)> Display to Students Example Question Using Array and Randomization The following table summarizes an actual question. QID 1950001 Name Template2 RAND.ARRAY Mode Multiple-Choice Question Answer Display to Students