The two easiest ways to assign a value to an array parameter are:
array_name=(value1 value2 ... valueN)
set -A array_name value1 value2 .. valueN
subscripts are used to select one of more individual items from an array. zsh's mechanism for subscripting into arrays is so powerful, it can be considered superior to many programming languages.
The simplest form is:
This uses arithmetic expansion to evaluate
an integer n. n is then used to return the
nth element from the array. (1)
Any subscript n that evaluates to a negative integer will take the nth element from the END of the array:
will return the second-to-last element in the
ARGV array parameter.
There are two special subscripts,
[*] that return a list of all of the items in an array.
The next form of subscripting is used to obtain multiple, consecutive elements in an array:
This returns elements (in
some_array) starting from
expr1, up to and including
expr2. Again, the
expressions are evaluated to integers before being used, and negative
numbers are used in the same way as descibed above.
Some quick examples:
> friends=(c_hong bongus hizatch sh_izzo sh_long mufastaf) > echo $friends c_hong bongus hizatch sh_izzo sh_long mufastaf > echo $friends[2,-1] bongus hizatch sh_izzo sh_long mufastaf > echo $friends[-4,-2] hizatch sh_izzo sh_long > echo $friends[-4,3] hizatch > echo $friends[5,-4] >
Subscripts can be included with variable names inside curly braces...
> veg=(lettuce carrot celery tomato onion) > echo $veg lettuce carrot celery tomato onion > veg=(pepper radish) > echo $veg lettuce carrot celery pepper radish onion
Subscripting may also be performed on non-array values. With this usage, the subscript specifies a substring to extract from a variable. (As opposed to a list of elements from an array)
> pain=cluster > echo $pain cluster > echo $pain u > echo $pain[2,5] lust > echo $pain[-6,7] luster > unset pain >