Array names start with an
Each produces the same array (list) of four items: the character
strings ``apple'', ``banana'', ``orange'',
and ``pear''. The first way is clear, and the second
somewhat more convenient using the white space between words to
delimit the elements of the array. Of course if one of the entries
was to be ``ripe bananas'', you would have to use the first method, or
you would produce an array of five elements.
The elements of the array are referred to as the zeroth, first,
second, etc. Symbolically, one writes $fruit[0],
$fruit[1], and so on. Note the switch from the
Arrays can also be specified in rather different ways:
The last example may be somewhat surprising. One might realistically
expect that @real_numbers is an array with two elements,
that latter being an array of seven integers, but no, that is not how
Perl works.
Another commonly used technique is to take a slice of an
array. Suppose @ALPHABET is the array (A, B, ...,
Z) ( or (A .. Z) using the .. construction).
Also, suppose that @slice is the array (0, 2, 3, 7).
Then @ALPHABET[@slice] is the array (A, C, D, H),
consisting of the 0th, 2nd, 3rd, and 7th elements of the array
@ALPHABET. This is a very handy construct for manipulating
problems in WeBWorK.