Basic Arrays

In Perl, all array variable names begin with @. For example:

my @list;   # An array of numbers

You can declare and initialize the array in one step:

my @list = ("Sam", "Joe", "Fred", "Sue");       # An array of names

Perl uses zero-based indexing just like C, but with a slight twist. The elements of the array are not

@list[0], @list[1], @list[2], @list[3]    # Not!

Instead, they are

$list[0], $list[1], $list[2], $list[3]

There is a good, if somewhat obscure, reason for this. Anything that begins with a dollar sign (“$”) results in a scalar. Anything that begins with an at sign (“@”) is an array. Finally, as you learn later, anything beginning with a percent sign (“%”) is something called a hash.

Because you are dealing with an array of ...

Get Perl for C Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.