< Day Day Up > |
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
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
12.7.1 Creating Arrays with the set CommandYou 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
|
< Day Day Up > |