Arrays & Lists
📚 Arrays & Lists
🏷️ The @ Sigil
@ marks an array — "more than one scalar, in order." But watch the sigil when accessing a single element:
This trips up nearly everyone coming from another language: @fruits is the array, but $fruits[0] — a single element out of it — uses $, not @. This isn't inconsistent; it's the same rule from Chapter 2 applied precisely: the sigil describes what you're asking for. $fruits[0] asks for one scalar (found inside the array called fruits); @fruits asks for the whole array. The variable's underlying name is always fruits — only the sigil in front changes to match what you're requesting.
🛠️ Common Array Operations
🔁 Iterating Arrays
$#fruits is the index of the last element of @fruits (not the count — one less than that) — 0..$#fruits is the idiomatic range covering every valid index.
🎭 List vs. Scalar Context
The same array, written identically, behaves differently depending on the context it's evaluated in:
Assigning to an array (@copy = ...) puts the right-hand side in list context — Perl treats @fruits as "all of its elements." Assigning to a scalar ($count = ...) puts it in scalar context — Perl treats the exact same @fruits as "how many elements does this have." Nothing about @fruits itself changed; only what surrounds it did.
Context isn't unique to arrays — it runs through much of Perl (Course 2's Context Deep Dive chapter covers it far more rigorously, including how your own subroutines can detect and respond to it). Arrays are simply the earliest, clearest place to encounter it: the exact same expression, two different meanings, decided entirely by what's asking for the value.
✂️ Array Slices
A slice pulls out multiple elements at once — and because the result is itself a list, the sigil is @, not $, even though it's written with square brackets like a single-element access.
🔀 Sorting and Reversing
This is the single most common Perl gotcha for beginners: sort @numbers compares elements as strings by default — "10" sorts before "2" because "1" is a "smaller" character than "2", exactly like sorting words alphabetically. For actual numeric order, pass a comparator block explicitly: sort { $a <=> $b } @numbers. <=> (sometimes called the "spaceship operator") is the numeric comparison Perl needs here — the default string comparator uses cmp instead.
Arrays: Perl vs. Python
Perl arrays and Python lists cover similar ground — ordered, growable collections with push/pop-style operations on both sides. The real differences are Perl-specific: the sigil switches from @ to $ the moment you access a single element, and the same array genuinely evaluates differently depending on list vs. scalar context — Python's len(my_list) is an explicit function call with no equivalent ambiguity, where Perl's my $count = @array; relies entirely on context to mean "count."
@ vs $
@array is the whole array; $array[0] is one scalar element.
push/pop/shift/unshift
Add/remove from the end or beginning.
List vs. scalar context
The same array means "all elements" or "count," depending on what surrounds it.
sort's default is string order
Use sort { $a <=> $b } @nums for numeric sorting.
💻 Coding Challenges
Challenge 1: Build and Access an Array
Declare an array of 4 favorite foods. Print the first and last elements using $array[0] and negative indexing, and print the total count using scalar(@array).
Challenge 2: List Context vs Scalar Context
Given my @nums = (1, 2, 3, 4, 5);, write one line assigning @nums to a new array (list context) and one line assigning it to a scalar (scalar context). Print both results and explain in one sentence why they differ.
Challenge 3: Numeric Sort
Given my @scores = (85, 9, 100, 42);, print the result of a plain sort @scores (demonstrating the string-sort gotcha), then print the correct numeric sort using a comparator block.
🎯 What's Next
Next chapter: Hashes — the % sigil, key-value operations, and iterating hashes.