Previous topic | Next topic | Ada Home Page | Index

Array Attributes

Array attributes give information about the array type or array variable.

AttributeMeaning
'FIRST the value of the smallest index
'LAST the value of the largest index
'RANGE the entire range of index values
'LENGTHthe number of items in the array

Examples

Given the following declarations (from recent examples):

    max_iarr : constant := 8;   -- largest index in int array
    min_farr : constant := 'a'; -- low  index in float array
    max_farr : constant := 'h'; -- high index in float array

    subtype STRING8 is STRING (1 .. 8);

    type int_8_array  is array (1 .. max_iarr)      of INTEGER;
    type float_arrays is array (min_farr..max_farr) of FLOAT;
    type str_arrays   is array (-5 .. 2)            of STRING8;
    type small_arrays is array ('a' .. 'c')         of FLOAT;

    arr1 : int_8_array;
    arr2 : float_arrays;
    arr3 : str_arrays;

    subtype lc_letters is CHARACTER range 'a' .. 'z';
    type freq_table is array (lc_letters) of INTEGER;

    count : freq_table := (others => 0); -- freq counts

we have the following attribute values:

Array type/variable and attribute Value
int_8_array'FIRST1
float_arrays'LAST'h'
str_arrays'RANGE-5 .. 2
arr3'LENGTH8
small_arrays'RANGE'a' .. 'c'
small_arrays'LENGTH3
freq_table'RANGElc_letters
'a' .. 'z'
count'RANGElc_letters


Array attributes in loops

A useful appplication of array attributes is setting the bounds of loop control variables:

    for t in count'RANGE loop
        PUT(t); PUT(count(t), width=>11); NEW_LINE;
    end loop;

This causes "t" to take each index value in turn for the array "count", regardless  of the index type and range.


Attributes of multi-dimensional arrays

The same attributes are used, with an index to show which dimension of the array is meant.

AttributeMeaning
'FIRST(n) the smallest value of the nth index
'LAST(n) the largest value of the nth index
'RANGE(n) the entire range of nth index values
'LENGTH(n)the number of items in the nth dimension of the array


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 27 Feb 96