Ad space (banner)
Lesson 8 / 20

Working with arrays (@)

This lesson covers the array, which handles several values together.

A Perl array is declared as @name. push adds an element and foreach takes every element out.

The sample code creates my @fruits = ();, adds fruit with push(@fruits, "apple"), and then displays every element with foreach my $f (@fruits).

A common early stumble is that the sigil changes: @fruits for the array as a whole, but $fruits[0] for a single element. "@ for the whole array, $ when you take one value out" is confusing at first and reads naturally once it clicks.

In professional work, arrays come up constantly for handling several pieces of data together — the rows of a CSV, the entries in a log.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Perl directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)