Skip to content
VLSI Mentor

Perl Part I · Module 5 · Arrays and Lists

Arrays: Ordered Lists of Scalars

Everything so far has held one value. An array holds several, in order, under one name — a list of block names, a column of marks, the fields of a line you have just split. The syntax has one feature that trips up every newcomer and is worth meeting head-on rather than discovering: a single array is referred to with two different sigils depending on what you are asking for. The at sign names the whole array; the dollar sign names one element out of it, because one element is a single value and single values take a dollar sign. The chapter also plants the hazard that will recur through the module: asking for an element that does not exist is not an error, not a warning, and not even visible.

Foundation13 min readPerlPart IFundamentalsArraysData Structures

Part I · Module 5 · Chapter 5.1 · Arrays: Ordered Lists of Scalars

1. Several Values Under One Name

Azvya Education Pvt. Ltd.VLSI Mentor
first-array.pl
use strict;
use warnings;

my @blocks = ("alu", "fifo", "decoder");

print "whole array : @blocks\n";
print "first       : $blocks[0]\n";
print "second      : $blocks[1]\n";
print "count       : ", scalar(@blocks), "\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
whole array : alu fifo decoder
first       : alu
second      : fifo
count       : 3

Four things in four lines:

  • my @blocks declares an array. The @ sigil is the one Chapter 2.1 introduced for "several values".
  • ("alu", "fifo", "decoder") on the right is a list — values separated by commas. The distinction between a list and an array matters and is Chapter 5.6's subject; for now, the list is what you assign and the array is what holds it.
  • "@blocks" inside a double-quoted string prints every element separated by single spaces.
  • scalar(@blocks) gives the count — the scalar() operator from Chapter 2.10, doing exactly the job it was introduced for.

The values are in the order you wrote them, and they stay in that order. That is the defining property: an array is ordered, so position means something.

2. One Array, Two Sigils

This is the piece to get right now, because every later chapter depends on it.

Azvya Education Pvt. Ltd.VLSI Mentor
two-sigils.pl
use strict;
use warnings;

my @blocks = ("alu", "fifo", "decoder");

print "the array   : @blocks\n";
print "one element : $blocks[0]\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
the array   : alu fifo decoder
one element : alu

Both lines refer to the same variable, with different sigils:

You writeYou getSigil, and why
@blocksthe whole array@ — several values
$blocks[0]one element$ — one value

The rule is not "arrays use @". The rule is that the sigil describes what the expression produces, which is the rule Chapter 2.1 actually stated. $blocks[0] produces one value, so it takes $. The [0] is what says which array and which position — the name plus the subscript.

3. An Array Can Hold Anything a Scalar Can

The canonical title says "ordered lists of scalars", and that is exact: every element is a scalar, so every element can be a number, a string, or undef — the dual nature from Chapter 2.6 applies element by element.

Azvya Education Pvt. Ltd.VLSI Mentor
mixed.pl
use strict;
use warnings;

my @row = ("alu_smoke_01", "PASS", 184, 7);

printf "name   : %s\n", $row[0];
printf "status : %s\n", $row[1];
printf "total  : %d\n", $row[2];
printf "fails  : %d\n", $row[3];
printf "count  : %d\n", scalar(@row);
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
name   : alu_smoke_01
status : PASS
total  : 184
fails  : 7
count  : 4

Two strings and two numbers in one array, and nothing special was needed. Perl has no array-of-one-type; an array is a sequence of scalars, and each scalar behaves as Chapter 3.5 described — the operator you apply decides how the value is read.

That is convenient and it is also why discipline matters: the array does not record which fields are quantities and which are labels. You do, in how you name it and which comparison operators you reach for.

4. An Empty Array Is a Normal Thing

Azvya Education Pvt. Ltd.VLSI Mentor
empty.pl
use strict;
use warnings;

my @failures = ();

print "contents: [@failures]\n";
print "count   : ", scalar(@failures), "\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output
contents: []
count   : 0

No warning, no error. An empty array is a perfectly ordinary state, and in this trade it is usually the state you want — an empty list of failures is a clean run.

my @failures; without the = () does the same thing. The explicit empty list says "this starts empty on purpose", which is the same argument Chapter 3.4 §3 made for initialising a string accumulator.

Chapter 5.7 is where the empty array becomes genuinely useful, because it turns out you can test an array directly without comparing its count to anything.

5. Reading Past the End Is Completely Silent

Here is the hazard to carry through the module.

Azvya Education Pvt. Ltd.VLSI Mentor
past-the-end.pl
use strict;
use warnings;

my @blocks = ("alu", "fifo", "decoder");

my $tenth = $blocks[10];

print "blocks[10] : [", (defined $tenth ? $tenth : "undef"), "]\n";
print "count now  : ", scalar(@blocks), "\n";

my $way_back = $blocks[-10];

print "blocks[-10]: [", (defined $way_back ? $way_back : "undef"), "]\n";
Azvya Education Pvt. Ltd.VLSI Mentor
Captured output — STDOUT
blocks[10] : [undef]
count now  : 3
blocks[-10]: [undef]
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet

Nothing on STDERR. Reading position 10 of a three-element array is not an error, not a warning, and does not change the array — the count is still 3.

Two consequences worth stating plainly:

  • The value you get is undef, which by Chapter 4.2 §6 interpolates into a string as nothing. So a report line built from an out-of-range field reads as a sentence with a gap in it, and the gap is one space.
  • The warning you do eventually get, if any, is in the wrong place. use warnings will complain when the undef is used — Use of uninitialized value — naming the line that printed it rather than the line that read past the end. If the value is passed around first, those two lines can be far apart.

6. Why This Matters in Engineering Work

A parsed line is an array. Every delimited row from Chapter 4.7 arrives as one, and from there the whole module applies: index it, slice it, count it, sort it.

Collecting results as you go. A script that walks a set of tests and records which failed is accumulating an array. Chapter 5.4 gives the function for that, and the empty-array-as-good-news point from §4 is what makes the final check read naturally.

Naming matters more with arrays than with scalars. @failures tells a reader both that it holds several things and what they are. @data tells them neither. And since §3 established that the array itself does not record whether its elements are quantities or labels, the name is carrying that information.

The silent read is a real source of quiet wrong answers, and it is worth internalising now because every container in Perl behaves this way. Hashes in Module 6 do the same. The habit from §5 — know the count before you index — is the one that scales.

7. Common Mistakes

Writing @blocks[0] for one element. That is a one-element slice. Perl warns and suggests $blocks[0].

Expecting an out-of-range read to fail. It gives undef, silently, and leaves the array unchanged.

Expecting the undef to be reported where it was read. The warning, if any, names the line that used it.

Assuming an array holds one type. Every element is a scalar; mixing numbers and strings is normal and is not checked.

Treating an empty array as an error. It is the usual good outcome for a list of problems.

Forgetting that "@array" interpolates with spaces between elements. That is useful for a quick look and wrong for anything you need delimited — use join from Chapter 4.7.

8. Exercise

Exercise 1 — declare and read. Create an array of four test names, print the whole array in a sentence, print the third one by itself, and print how many there are. Say which sigil each of those three lines needed and why.

Exercise 2 — produce the slice warning. Print one element using @names[0] instead of $names[0]. Record the exact warning text and the value you got, then say in one clause what @names[0] actually is.

Exercise 3 — mixed contents. Build an array containing a test name, a status word and two counts. Print each with an appropriate printf conversion from Chapter 4.8, then state what the array itself knows about which fields are numbers.

Exercise 4 — the silent read. With a three-element array, read position 7 into a variable and print whether it is defined, plus the array's count. Capture STDERR separately and confirm it is empty. Then print the value inside a sentence and record what appears and what STDERR says this time.

Exercise 5 — guard the index. Write a script with my @fields = ("alu", "PASS"); that wants a fourth field. Add the count check from §5 so it reports a short row instead of reading undef, and confirm the message fires.

Exercise 6 — the empty case. Declare an empty array two ways — = () and no initialiser — and print the count and the interpolated contents of each. Say whether they differ, and which form you would commit.

Exercise 7 — interpolation versus join. Given an array of three block names, print it once with "@blocks" and once with join(",", @blocks). Say which you would write into a CSV file and why the other one is wrong for that.

9. Summary

  • An array holds several scalars in order under one name, declared with my @name.
  • One array, two sigils: @blocks is the whole array, $blocks[0] is one element — because the sigil describes what the expression produces.
  • @blocks[0] is a one-element slice, not an element, and Perl warns with the fix.
  • Every element is a scalar, so mixed numbers and strings are normal and untyped.
  • "@array" interpolates space-separated; use join when you need a specific delimiter.
  • An empty array is ordinary, and usually the good outcome for a list of problems.
  • Reading past the end gives undef silently — no warning, no error, and the array is not extended. Know the count before you index.

Chapter 5.2 covers positions properly: counting from both ends, and the two different ways of asking "how big is this array" that differ by exactly one and catch people constantly.

Where this fits

Part of the Perl curriculum.