220 10 Transforming with Traditional Programming Languages
number and legal obligations, almost as if it were a person. There are similar
situations in biology as well. Multicellular organs and organisms are living
entities that are made up of cells but which act as if they were single units.
Perl arrays are made into single entities (scalars) by using brackets. For ex-
ample, the array@linescould be made into a scalar by writing[@lines],
and one can assign such an entity to a scalar variable as in
$var = [@lines];
One can put any number of arrays and scalars in brackets, and the result is
(reference to) a single array. Hashes are made into single entities by using
braces. One can combine hashes by putting more than one in braces, and
one can add additional keys as in
$var = {
name => "George",
id => "123456",
%otherData,
};
In this case$varrefers to a hash that maps “name” to “George” and “id” to
“123456,” in addition to all of the other mappings in%otherData.
Now consider the same statistical task as in program 10.3; namely, com-
pute the mean and standard deviation of the BMI. However, now use a
database table rather than computing it as the file is being read to obtain
program 10.7.
The statistical computation is done in theforstatement. This is similar to
theforeachstatement. Whereas theforeachstatement iterates over the
items of a list, theforstatement iterates over the numbers in a sequence.
The statement specifies the three parts of any such sequence: where to start
it, how to end it, and how to go from one number in the sequence to the next
one. The three parts in this case specify:
- Where to start:$i = 0means start the sequence at 0.
- How to end: $i < $countmeans to stop just before the number of
records in the database. The reason for ending just before the number
of records rather than at the number of records is that numbering starts at
0. - How to go from one number to the next:$i++means increment the num-
ber to get the next one. In other words, the sequence is consecutive.