Perl Basics   «Prev  Next»

Lesson 13Aggregate data types
ObjectiveLearn what the two basic aggregate data types are in Perl.

Perl Aggregate Data Types

Sometimes it does not suffice to store a single dimension of data. Many times data come in groups, in pluralities, like raindrops beating on the tin roof of the carport while you're trying to sleep. For these purposes, Perl has two basic aggregate data types:
  1. arrays and
  2. hashes.

What are the Aggregate Data Types in the Perl programming language?

Perl supports several aggregate data types, which are data structures that can store multiple values in a single data type. The following are the most commonly used aggregate data types in Perl:
  1. Arrays: An array is a list of scalar values that can be accessed using a numeric index. Arrays in Perl are dynamically sized, meaning that they can grow or shrink as needed.
  2. Hashes: A hash is an associative array that uses a string key to access its values. Hashes in Perl allow you to store and retrieve values based on a string key, rather than a numeric index.
  3. Scalar References: A scalar reference is a scalar value that holds a reference to another data type. This allows you to store complex data structures, such as arrays and hashes, within a scalar value.
  4. Array References: An array reference is a scalar value that holds a reference to an array. This allows you to pass arrays to subroutines, store arrays in hashes, or create arrays of arrays.
  5. Hash References: A hash reference is a scalar value that holds a reference to a hash. This allows you to pass hashes to subroutines, store hashes in arrays, or create hashes of hashes.

These aggregate data types provide a flexible and powerful way to store and manipulate data in Perl, and they are widely used in a variety of applications.


Array

The simpler form is the array. It is simply a two-dimensional scalar.
Indexed numerically, starting at zero, the contents of a Perl array is simply an ordered list of scalar variables.
The more complex form is the hash (also called associative array, but it is easier to say hash. A hash is like an array, but it is indexed with scalar values instead of numbers.
The next lesson will go into detail about working with arrays, and the lesson following that will explore hashes.

In Perl, an array is an ordered list of scalars. The following is how you might assign a few numbers to an array:
my @even = ( 2, 4, 6, 8, 10 );
For an array, the variable is preceded by an @ (at) sign, so the mnemonic for this is an @array.
When you have a list of items, separate them with a comma and use parentheses () around them.
So, the preceding code has an array on the left and a list on the right, and you are assigning the list to the array. Of course, just as with scalars, you can use any kind of data you need to use. You can even assign lists with a mix of strings or other scalars to an array:
my $nine = 9;
my @stuff = ( 7, 'of', $nine );

If you just print the array as shown in the following example, what happens?
my $nine = 9;
my @stuff = ( 7, 'of', $nine );
print @stuff, '\n';
print '@stuff\n';

Although there are ways you can tweak this, the code just presented will probably print the following:
7of9
7 of 9