Arrays are series of values. For instance the days of the week, the months of the year, daily rainfall measurements, hourly temperatures etc. Those values can be grouped as you in the following declarations:

Individual values are accessed by their indices. For instance in the weekday array the first value ("Monday") is printed:

When individual values are used the @-sign is replaced by the $-sign and the index is enclosed in square brackets []. Please note that the elements in the array are numbered starting with 0. The last day in the week has the index 6. It is also possible to assign values to individual elements of an array:

This element with index 7 has not been defined in the above declaration. As soon as this assigment is executed the element will be created, the array @rain will be elongated.

A simple way to test for the current number elements in an array is to asign the array to a scalar variable:

The variable $no_of_elements_in_array will then contain exactly this namely the number of elements in the array @weekday, that is 7.

The for-loop allows repetitive calculations. The number of repetitions is determined by a loop-variable ($n). In the following example the mean rain fall over seven days is calculated:

$n is the loop variable. This variable is first initialized with 0. Then a condition is defined which must be true in order for the following block to be executed. In the third part of the for-statement the loop variable is incremented. During execution time the loop variable is initialized only once. Then condition is tested and if it comes out to be true then the "block", i.e. the statements which follow between the wavy brackets {} are executed. Then control goes back to the for-statement, the loop-variable is incremented (from 0 to 1) and again the condition is tested. If true another round is started, repeating the statements in the block with the new value of $n. This is then repeated until at some time the condition will no longer be true, thast is when $n hast reached the value of 7. Then the execution is continued with the first statement after the block, where the $sum is divided by the number of days resulting in the mean rain fall during this week.

Please note also that the "block" is indented. This no necessary PERL feature but simply a habit you should develop to mark statements in blocks. This greatly enhances the readability and maintainability of your programs, especially when block are "nested", i.e. when blocks contain other blocks and you have to to locate the corresponding brackets. Missing or wrong placed brackets are a common source of programming errors.

We continue to calculate the standard deviation of a set of values. The
following formula has been taken from a statistics textbook and shows the
variance of a set of values. The standard deviation (s) is the square root of the
variance (s^{2}).

Again we can use a for-loop for the calculation. **sqrt** is a predefined
PERL-routine for the square root and ** is the potentiation symbol:

The while-loop is controlled by a single condition:

You have to care yourself for incrementation of the loop-variable ($n). If you forget this, then the while-loop will never end. In the example above the days of the week are printed after the number of days in the array @weekday have been determined.

© 2001-2007, by Kurt Stüber.

This page is part of a PERL course for bioinformatics, written by Kurt Stüber. Copyright 2007 by Kurt Stüber.