10.8. Arrays

10.8.1. What Is an Array?

In the TC shell, an array is simply a list of words, separated by spaces or tabs, and enclosed in parentheses. The elements of the array are numbered by subscripts starting at one. If there is not an array element for a subscript, the message "Subscript out of range" is displayed. Command substitution will also create an array. If the $# notation precedes an array name, the number of elements in the array is displayed.

Example 10.71.
1  > set fruit = ( apples pears peaches plums )
2  > echo $fruit
						apples pears peaches plums

3  > echo $fruit[1]
						Subscripts start at 1
						apples

4  > echo $fruit[2–4]
						Prints the 2nd, 3rd, and 4th elements
						pears peaches plums

5  > echo $fruit[6]
						Subscript out of range.

6  > echo $fruit[*]
						Prints all elements of the array
						apples pears peaches plums

7  > echo $#fruit
						Prints the number of elements
						4

8  > echo $%fruit
						Prints the number of characters in the list
						23

9  > echo $fruit[$#fruit]
						Prints the last element
						plums

10 > set fruit[2] = bananas
						Reassigns the second element
   > echo $fruit
						apples bananas peaches plums

11 > set path = ( ~ /usr/bin /usr /usr/local/bin . )
   > echo $path
						/home/jody/ellie /usr/bin /usr /usr/local/bin .

12 > echo $path[1]
						/home/jody/ellie
					

Explanation

  1. The wordlist is enclosed within parentheses. Each word is separated by white space. The array is called fruit.

  2. The words in thefruit array are printed.

  3. The first element of the fruit array is printed. The subscripts start at one.

  4. The second, third, and fourth elements of the wordlist are printed. The dash allows you to specify a range.

  5. The array does not have six elements. The subscript is out of range.

  6. All elements of thefruit array are printed.

  7. The $# preceding the array is used to obtain the number of elements in the array. There are four elements in the fruit array.

  8. The $% preceding a variable or an array prints the number of characters in the word(s).

  9. Because the subscript $#fruit evaluates to the total number of elements in the array, if that value is used as an index value of the array, i.e., [$#fruit ], the last element of the fruit array is printed.

  10. The second element of the array is assigned a new value. The array is printed with its replaced value, bananas.

  11. The path variable is a special C shell array of directories used to search for commands. When you create an array, the individual elements of the path can be accessed or changed.

  12. The first element of path is printed.

The shift Command and Arrays

If the built-in shift command takes an array name as its argument, it shifts off (to the left) the first element of the array. The length of the array is decreased by one. Without an argument, the shift command shifts off the first element of the built-in argv array.

Table 10.20. Variable Modifiers
Special Modifier Example What It Means
$? $?var Returns 1 if var is set; 0 if not.
$# $#var Returns the number of words in var.
$% $%var Returns the number of characters in var.

Example 10.72.
1  > set names = ( Mark Tom Liz Dan Jody )

2  > echo $names
							Mark Tom Liz Dan Jody

3  > echo $names[1]
							Mark

4  > shift  names

5  > echo $names
							Tom Liz Dan Jody

6  > echo $names[1]
							Tom

7  > set days = ( Monday Tuesday )

8  > shift days

9  > echo $days
							Tuesday

10 > shift days

11 > echo $days

12 > shift days
							shift: no more words.
						

Explanation

  1. The array is callednames. It is assigned the list of words in parentheses. Each word is separated by white space.

  2. The array is printed.

  3. The first element of the array is printed.

  4. The array is shifted to the left by one element. The word Mark is shifted off.

  5. The array was decreased by one element after the shift.

  6. The first element of the array, after the shift, is Tom.

  7. An array, called days, is created. It has two elements, Monday and Tuesday.

  8. The array, days, is shifted one to the left.

  9. The array is printed. Tuesday is the only element left.

  10. The array, days, is shifted again. The array is empty.

  11. The days array is empty.

  12. This time, attempting to shift causes the shell to send an error message indicating that it cannot shift elements from an empty array.

Creating an Array from a String

You may want to create a wordlist out of a quoted string. This is accomplished by placing the string variable within a set of parentheses.

Example 10.73.
1  > set name = "Thomas Ben Savage"
							echo $name[1]
							Thomas Ben Savage

2  > echo $name[2]
							Subscript out of range.

3  > set name = ( $name )

4  > echo $name[1] $name[2] $name[3]
							Thomas Ben Savage
						

Explanation

  1. The variable name is assigned the string "Thomas Ben Savage."

  2. When treated as an array, there is only one element, the entire string.

  3. The variable is enclosed in parentheses, creating an array of words, called name.

  4. The three elements of the new array are displayed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.191.86