Lists, Arrays, and Hashes
List Assignment
List assignment copies several values into several scalar variables in order.
list assignment
List assignment places each value from the right side into the matching variable on the left side.
List Assignment
list_assignment.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $middle = "green";
my ($first, $second, $third) = ("red", $middle, "blue");
my $summary = "$first,$second,$third";
print "first=$first\n";
print "second=$second\n";
print "summary=$summary\n";
use strict;
use warnings;
my $middle = "yellow";
my ($first, $second, $third) = ("red", $middle, "blue");
my $summary = "$first,$second,$third";
print "first=$first\n";
print "second=$second\n";
print "summary=$summary\n";
use strict;
use warnings;
my $middle = "purple";
my ($first, $second, $third) = ("red", $middle, "blue");
my $summary = "$first,$second,$third";
print "first=$first\n";
print "second=$second\n";
print "summary=$summary\n";
$middle ← green, $first ← red, $second ← green, $third ← blue
4my $middle→ green = "green"; #@middle="yellow", "purple"5my ($first→ red, $second→ green, $third→ blue) = ("red", $middlegreen, "blue");6my $summary→ red,green,blue = "$firstred,$secondgreen,$thirdblue";78print "first=$firstred\n";9print "second=$secondgreen\n";10print "summary=$summaryred,green,blue\n";outputfirst=red second=green summary=red,green,blue
$middle ← yellow, $first ← red, $second ← yellow, $third ← blue
4my $middle→ yellow = "yellow";5my ($first→ red, $second→ yellow, $third→ blue) = ("red", $middleyellow, "blue");6my $summary→ red,yellow,blue = "$firstred,$secondyellow,$thirdblue";78print "first=$firstred\n";9print "second=$secondyellow\n";10print "summary=$summaryred,yellow,blue\n";outputfirst=red second=yellow summary=red,yellow,blue
$middle ← purple, $first ← red, $second ← purple, $third ← blue
4my $middle→ purple = "purple";5my ($first→ red, $second→ purple, $third→ blue) = ("red", $middlepurple, "blue");6my $summary→ red,purple,blue = "$firstred,$secondpurple,$thirdblue";78print "first=$firstred\n";9print "second=$secondpurple\n";10print "summary=$summaryred,purple,blue\n";outputfirst=red second=purple summary=red,purple,blue
Assign in Order
$middlestarts asgreen.- The right-side list is
red,green,blue. - Perl assigns each value to the matching variable on the left.
- The summary joins the three scalar values with commas.
| Position | Variable | Value |
| --- | --- | --- |
|
1|$first|red| |2|$second|green| |3|$third|blue|
Exercise: list_assignment.pl
Assign three list values into scalar variables and print a comma-separated summary