2.1 Basic Perl Data Types
Before tackling references, let's review the basic Perl data types:
A scalar value is a string or any one of several kinds of numbers such as integers, floating-point (decimal) numbers, or
in scientific notation such as 2.3E23. A scalar variable begins with the dollar sign
, as in
An array is an ordered collection of scalar values. An array variable begins with an at sign
, as in
. An array can be
by a list such as
. Individual scalar elements of an array are referred to by first
the array name with a dollar sign (an individual element of an array is a scalar value) and then following the array
with the position of the desired element in square brackets. Thus the first element of the
array is referenced by
and has the value '
'. (Note that array elements are given the
0, 1, 2, ...,
is the number of elements in the array.)
Recall that printing an array within double quotes causes the elements to be separated by spaces; without the double quotes, the elements are printed one after the other without separations. This snippet:
@pentamers = ('cggca', 'tgatc', 'ttggc');
print "@pentamers", "\n";
print @pentamers, "\n";
produces the output:
cggca tgatc ttggc
A hash is an unordered collection of key value pairs of scalar values. Each scalar
is associated with a scalar value. A hash variable begins with the percent sign
, as in
. A hash can be initialized like an array, except that each pair of scalars are taken as a key with its value, as in:
symbol is just a synonym for a comma that makes it easier to see the key/value pairs in such lists.
An individual scalar value is retrieved by preceding the hash name with a dollar sign (an individual value is a scalar value) and following the hash name with the key in curly braces, as in
, which, because of how it's initialized, has the value '
It also forces the left side to be interpreted as a string.