Use Perl Hash Variables

Perl hashes are special kinds of arrays consisting of named key/value pairs. You can return a value by specifying the corresponding key.

Perl hash variables are prefixed by the percent sign (%) character. 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.

Hashes

Hashes are like a lookup table with a key column and a value column. Instead of specifying a numeric index, you use the key as an index to find the corresponding value.

Key Value
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, separate rows with commas, and use the => operator between the key and the value. For example:

%planets = ( 'Mercury' => 0.39 ,
             'Venus'   => 0.72 ,
             'Earth'   => 1    , 
             'Mars'    => 1.52 ,
             'Jupiter' => 5.20 ,
             'Saturn'  => 9.54 ,
             'Uranus'  => 19.18,
             'Neptune' => 30.06 );
Best Practice Initialize hashes and other variables at the very beginning of Question.

You can add a single key/value pair to your hash by specifying the hash variable as a scalar followed by the key name in braces. For example:

$planets{'Neptune'} = 30.06; 
Note Because this is a simple assignment statement, you use = and not =>.

To look up a value in a hash variable, you also specify the hash variable as a scalar followed by the key name in braces. For example:

The average orbital distance of Venus is <eqn $planets{'Venus'}> AU.
  1. In an <EQN> or <eqn> tag, set the keys and values for the hash as in the following examples.
    %elements = ( 'H'  => 'Hydrogen', 
                  'He' => 'Helium', 
                  'Li' => 'Lithium', 
                  'Be' => 'Beryllium', 
                  'B'  => 'Boron',
                  'C'  => 'Carbon',
                  'N'  => 'Nitrogen',
                  'O'  => 'Oxygen',
                  'F'  => 'Fluorine',
                  'Ne' => 'Neon' );
    $elements{'Na'} = 'Sodium';
    %oxygen = ( 'symbol' => 'O', 
                'number' => 8, 
                'weight' => 15.9994, 
                'econfig' => '1s<sup>2</sup> 2s<sup>2</sup> 2p<sup>4</sup> ');
    Best Practice Initialize arrays and other variables at the very beginning of Question.
  2. In an <EQN> or <eqn> tag, look up hash values by specifying the hash variable as a scalar followed by the key name in braces, as in the following examples.
    $element_name = $elements{'He'};
    # sets $element_name = Helium
    
    if ($this_weight == $oxygen{'weight'}) {$my_element = 'Oxygen'}; 
    # compares $this_weight and 15.9994

Example Question Using Hash Variables

The following table summarizes an actual question.

QID
Name
Mode Fill-in-the-Blank
Question
<eqn>
$keyA = "He\tNe\tAr\tKr\tXe\tRn";  # \t creates a {tab} in the answer key.
$keyB = $keyA;
%names =   ( 'He' => 'Helium',
             'Ne' => 'Neon',
             'Ar' => 'Argon',
             'Kr' => 'Krypton',
             'Xe' => 'Xenon',
             'Rn' => 'Radon');
%numbers = ( 'He' => 2,
             'Ne' => 10,
             'Ar' => 18,
             'Kr' => 36,
             'Xe' => 54,
             'Rn' => 86);
%econfig = ( 'He' => '1s^{2}',
             'Ne' => '1s^{2} 2s^{2} 2p^{6}',
             'Ar' => '[Ne] 3s^{2} 3p^{6}',
             'Kr' => '[Ar] 3d^{10} 4s^{2} 4p^{6}',
             'Xe' => '[Kr] 5s^{2} 4d^{10} 5p^{6}',
             'Rn' => '[Xe] 4f^{14} 5d^{10} 6s^{2} 6p^{6}');
''
</eqn>
Provide the following information for any two of the six naturally-occurring noble gases:<br><br>
<table border=1 rules="all">
<thead style="background-color: #9DC2DF;">
<tr><td>Symbol</td><td>Name</td><td>Atomic Number</td><td>Electron Configuration</td><tr>
</thead>
<tbody>
<tr style="vertical-align: top;"><td><_></td><td><_></td><td><_></td><td><_></td></tr>
<tr style="vertical-align: top;"><td><_></td><td><_></td><td><_></td><td><_></td></tr>
</tbody>
</table>
Answer
<EQN $CASE=1; $A=$thisresponse; $keyA>
<EQN $names{$A}>
<EQN $numbers{$A}>
<EQN $PAD='chem'; $CHEM='econf,either'; $econfig{$A}>
<EQN $CASE=1; $B=$thisresponse; $keyB =~ s/($A)//; $keyB>
<EQN ($A eq $B) ? '' : $names{$B}>
<EQN ($A eq $B) ? '' : $numbers{$B}>
<EQN $PAD='chem'; $CHEM='econf,either'; ($A eq $B) ? '' : $econfig{$B}>
Display to Students
Question as displayed to students