Previous Section  < Day Day Up >  Next Section

12.7. Arrays

Korn shell arrays are one-dimensional arrays that may contain up to 1,024 (size varies) elements consisting of words or integers. The index starts at 0. Each element of an array can be set or unset individually. Values do not have to be set in any particular order. For example, you can assign a value to the tenth element before you assign a value to the first element. An array can be set using the set command with the –A option.

Associative arrays are supported under versions of the Korn shell that are more recent than 1988.

Example 12.50.

(The Command Line)

1    $ array[0]=tom

     $ array[1]=dan

     $ array[2]=bill

2    $ print ${array[0]}     # Curly braces are required.

     tom

3    $ print ${array[1]}

     dan

4    $ print ${array[2]}

     bill

5    $ print ${array[*]}     # Display all elements.

     tom dan bill

6    $ print ${#array[*]}    # Display the number of elements.

     3


EXPLANATION

  1. The first three elements of the array are assigned values. The index starts at 0.

  2. The value of the first array element, tom, is printed. Make sure you remember to surround the variable with curly braces. $array[0] would print tom[0].

  3. The value of the second element of the array, dan, is printed.

  4. The value of the third element of the array, bill, is printed.

  5. All elements in the array are printed.

  6. The number of elements in the array is printed. An array can be declared with typeset if you know the size and type.

Example 12.51.

(At The Command Line)

1    $ typeset –i ints[4]     # Declare an array of four integers.

2    $ ints[0]=50

     $ ints[1]=75

     $ ints[2]=100

3    $ ints[3]=happy

     ksh: happy: bad number


EXPLANATION

  1. The typeset command creates an array of 4 integers.

  2. Integer values are assigned to the array.

  3. A string value is assigned to the fourth element of the array, and the Korn shell sends a message to standard error.

12.7.1 Creating Arrays with the set Command

You can assign the values of an array using the set command. The first word after the –A option is the name of the array; the rest of the words are the elements of the array.

Example 12.52.

(The Command Line)

1   $ set –A fruit apples pears peaches

2   $ print ${fruit[0]}

    apples

3   $ print ${fruit[*]}

    apples pears peaches

4   $ fruit[1]=plums

5   $ print ${fruit[*]}

    apples plums peaches


EXPLANATION

  1. The set command with the –A option creates an array. The name of the array, fruit, follows the –A option. Each of the elements of the fruit array follow its name.

  2. Subscripts start at 0. Curly braces are required around the variable for it to be evaluated properly. The first element of the array is printed.

  3. When the asterisk is used as a subscript, all elements of the array are displayed.

  4. The second element of the array is reassigned the value plums.

  5. All elements of the array are displayed.

    Previous Section  < Day Day Up >  Next Section