Like most programming languages, Perl knows about different varieties of data. In this tutorial we’ll explore the most common data types used in Perl:
- Scalars
- Arrays (lists)
- Associative arrays (hashes)
Scalars
Scalar variables are used to store single values – for example, a number or a string. They are preceded by the dollar sign ($
). Here are some examples of scalars:
$first_name = "Jacob";
$total_widgets = 43;
$price = 12.99;
Unlike many other languages, Perl does not distinguish between the storage of strings, integers and decimal numbers. They are all just scalars as far as Perl is concerned.
Instead, the context in which you use a scalar determines how it will be interpreted. For example, if you are joining two scalars together using the string concatenation operator (.
), the scalars will be interpreted as strings. On the other hand, if you add two scalars using the addition operator (+
), Perl will try to interpret the scalars as numbers.
Arrays (lists)
Arrays, often called lists, can store a list of many scalar values at once. They are preceded by the at sign (@
). Here are some arrays in Perl:
@names = ( "Mary", "Jim", "Fred" );
@salaries = ( 54000, 22500, 45000 );
@prices = ( 14.99, 19.99, 29.99, 39.99, 59.99 );
You can access a single element in an array using the numeric index (position) of the element. In Perl, the first element of an array has the index 0, not 1 as you might expect.
Here are some examples, using the arrays we defined above:
print $names[0];
(prints “Mary”)
print $salaries[1];
(prints “22500”)
print $prices[2];
(prints “29.99”)
Notice that each array element (e.g. $names[0]
) uses a $
symbol rather than an @
symbol. This is because each element is a scalar, not an array.
Slicing an array
We can also pull out several elements of an array in a row, to form a smaller array. This is known as extracting a slice from an array. For example:
@slice = @prices[2..4];
(@slice
now holds the values ( 29.99, 39.99, 59.99 ) ).
The code @prices[2..4]
means “give me elements 2 to 4 inclusive from the array @prices
“.
Associative arrays (hashes)
The final data type we’re going to cover here is the associative array, often called a hash. Associative arrays are similar to normal arrays, with one important difference. Instead of using numbers to index each element in an array, you can use more meaningful strings.
Hash variables are preceded by the percent sign (%
).
Here are some examples of hashes:
%favourite_cats = ( mary => "Fluffy", jim => "Tibby", fred => "Lucky" );
%colour_purple = ( r => 255, g => 0, b => 255 );
%prices = (
"Super Widget" => 39.99,
"Wonder Widget" => 49.99,
"Mega Widget" => 69.99
);
Some things to note:
- First of all, each element (e.g.
"Fluffy"
) is referenced by a string index (e.g.mary
). This index is often called a key, and the corresponding element is called a value. - When defining a hash using the
key => value
syntax above, you don’t need to put quotes around the key, unless there are spaces or other non-word characters in the key (as in the%prices
example). - For readability, you can put each key/value pair on a separate line, as shown in the
%prices
example above.
To access values in a hash, you use the syntax $hash{$key}
. For example:
print $favourite_cats{mary};
(prints “Fluffy”)
print $colour_purple{g};
(prints “0”)
print $prices{"Mega Widget"};
(prints “69.99”)
As with arrays, notice that the hash values are scalars, so they start with a $
(for example, $favourite_cats{mary}
).
Leave a Reply