Arrays store ordered values and can be read by index.

array index Array positions start at zero, so `$scores[0]` reads the first value.

Arrays

bonus
arrays.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;

my @scores = (82, 91, 76);
my $bonus = 5;
my $first_score = $scores[0];
my $adjusted_score = $scores[1] + $bonus;

print "first=$first_score\n";
print "adjusted=$adjusted_score\n";
use strict;
use warnings;

my @scores = (82, 91, 76);
my $bonus = 0;
my $first_score = $scores[0];
my $adjusted_score = $scores[1] + $bonus;

print "first=$first_score\n";
print "adjusted=$adjusted_score\n";
use strict;
use warnings;

my @scores = (82, 91, 76);
my $bonus = 10;
my $first_score = $scores[0];
my $adjusted_score = $scores[1] + $bonus;

print "first=$first_score\n";
print "adjusted=$adjusted_score\n";
  1. @scores ← 3, $bonus ← 5, $first_score ← 82, $adjusted_score ← 96

    4my @scores→ 3 = (82, 91, 76);5my $bonus→ 5 = 5; #@bonus=0, 106my $first_score→ 82 = $scores[0]82;7my $adjusted_score→ 96 = $scores[1]91 + $bonus5;89print "first=$first_score82\n";10print "adjusted=$adjusted_score96\n";
    outputfirst=82
    adjusted=96
  1. @scores ← 3, $bonus ← 0, $first_score ← 82, $adjusted_score ← 91

    4my @scores→ 3 = (82, 91, 76);5my $bonus→ 0 = 0;6my $first_score→ 82 = $scores[0]82;7my $adjusted_score→ 91 = $scores[1]91 + $bonus0;89print "first=$first_score82\n";10print "adjusted=$adjusted_score91\n";
    outputfirst=82
    adjusted=91
  1. @scores ← 3, $bonus ← 10, $first_score ← 82, $adjusted_score ← 101

    4my @scores→ 3 = (82, 91, 76);5my $bonus→ 10 = 10;6my $first_score→ 82 = $scores[0]82;7my $adjusted_score→ 101 = $scores[1]91 + $bonus10;89print "first=$first_score82\n";10print "adjusted=$adjusted_score101\n";
    outputfirst=82
    adjusted=101

What Happens

  1. @scores starts as 82, 91, and 76.
  2. $bonus starts at 5.
  3. $scores[0] reads 82.
  4. $scores[1] + $bonus adds 91 + 5.
  5. The program prints first=82 and adjusted=96.

Array Picture

| bonus | first value | adjusted value | | --- | --- | --- | | 0 | 82 | 91 | | 5 | 82 | 96 | | 10 | 82 | 101 |

Try It

Exercise: arrays.pl

Reproduce first=82 and adjusted=96, then use the pinned bonuses 0 and 10 to predict each adjusted value.