1# Test parameter subscripting.
2
3%prep
4
5  s='Twinkle, twinkle, little *, [how] I [wonder] what?  You are!'
6  a=('1' ']'  '?' '\2'  '\]' '\?'  '\\3' '\\]'  '\\?' '\\\4'  '\\\]' '\\\?')
7  typeset -g -A A
8  A=($a)
9
10%test
11
12  x=','
13  print $s[(i)winkle] $s[(I)winkle]
14  print ${s[(i)You are]} $#s
15  print ${s[(r)$x,(R)$x]}
160:Scalar pattern subscripts without wildcards
17>2 11
18>53 60
19>, twinkle, little *,
20
21  x='*'
22  print $s[(i)*] $s[(i)\*] $s[(i)$x*] $s[(i)${(q)x}*] $s[(I)$x\*]
23  print $s[(r)?,(R)\?] $s[(r)\?,(R)?]
24  print $s[(r)\*,(R)*]
25  print $s[(r)\],(R)\[]
260:Scalar pattern subscripts with wildcards
27>1 26 1 26 26
28>Twinkle, twinkle, little *, [how] I [wonder] what? ?  You are!
29>*, [how] I [wonder] what?  You are!
30>] I [
31
32  print $s[(i)x] : $s[(I)x]
33  print $s[(r)x] : $s[(R)x]
340:Scalar pattern subscripts that do not match
35>61 : 0
36>:
37
38  print -R $s[$s[(i)\[]] $s[(i)$s[(r)\*]] $s[(i)${(q)s[(r)\]]}]
390:Scalar subscripting using a pattern subscript to get the index
40>[ 1 33
41
42  print -R $a[(r)?] $a[(R)?]
43  print $a[(n:2:i)?] $a[(n:2:I)?]
44  print $a[(i)\?] $a[(I)\?]
45  print $a[(i)*] $a[(i)\*]
460:Array pattern subscripts
47>1 ?
48>2 2
49>3 3
50>1 13
51
52  # It'd be nice to do some of the following with (r), but we run into
53  # limitations of the ztst script parsing of backslashes in the output.
54  print -R $a[(i)\\\\?] $a[(i)\\\\\?]
55  print -R $a[(i)\\\\\\\\?] $a[(i)\\\\\\\\\?]
56  print -R ${a[(i)\\\\\\\\?]} ${a[(i)\\\\\\\\\?]}
57  print -R "$a[(i)\\\\\\\\?] $a[(i)\\\\\\\\\?]"
58  print -R $a[(i)\]] $a[(i)\\\\\]] $a[(i)\\\\\\\\\]] $a[(i)\\\\\\\\\\\\\]]
59  print -R $a[(i)${(q)a[5]}] $a[(i)${(q)a[8]}] $a[(i)${(q)a[11]}]
60  print -R $a[(i)${a[3]}] $a[(i)${a[6]}] $a[(i)${a[9]}] $a[(i)${a[12]}]
610:Array pattern subscripts with multiple backslashes
62>4 6
63>7 9
64>7 9
65>7 9
66>2 5 8 11
67>5 8 11
68>1 3 4 6
69
70  print -R $A[1] $A[?] $A[\\\\3] $A[\\\]]
71  print -R $A[$a[11]]
72  print -R $A[${(q)a[5]}]
730:Associative array lookup (direct subscripting)
74>] \2 \\] \?
75>\\\?
76>\\\?
77
78  # The (o) is necessary here for predictable output ordering
79  print -R $A[(I)\?] ${(o)A[(I)?]}
80  print -R $A[(i)\\\\\\\\3]
81  print -R $A[(I)\\\\\\\\\?] ${(o)A[(I)\\\\\\\\?]}
820:Associative array lookup (pattern subscripting)
83>? 1 ?
84>\\3
85>\\? \\3 \\?
86
87  print -R $A[(R)\?] : ${(o)A[(R)?]}
88  print -R $A[(R)\\\\\?] ${(o)A[(R)\\\\?]} ${(o)A[(R)\\\\\?]}
89  print -R ${(o)A[(R)\\\\\\\\\]]}
900:Associative array lookup (reverse subscripting)
91>: ]
92>\? \2 \? \?
93>\\]
94
95  eval 'A[*]=star'
961:Illegal associative array assignment
97?(eval):1: A: attempt to set slice of associative array
98
99  x='*'
100  A[$x]=xstar
101  A[${(q)x}]=qxstar
102  print -R ${(k)A[(r)xstar]} $A[$x]
103  print -R ${(k)A[(r)qxstar]} $A[${(q)x}]
104  A[(e)*]=star
105  A[\*]=backstar
106  print -R ${(k)A[(r)star]} $A[(e)*]
107  print -R ${(k)A[(r)backstar]} $A[\*]
1080:Associative array assignment
109>* xstar
110>\* qxstar
111>* star
112>\* backstar
113
114  o='['
115  c=']'
116  A[\]]=cbrack
117  A[\[]=obrack
118  A[\\\[]=backobrack
119  A[\\\]]=backcbrack
120  print -R $A[$o] $A[$c] $A[\[] $A[\]] $A[\\\[] $A[\\\]]
121  print -R $A[(i)\[] $A[(i)\]] $A[(i)\\\\\[] $A[(i)\\\\\]]
1220:Associative array keys with open and close brackets
123>obrack cbrack obrack cbrack backobrack backcbrack
124>[ ] \[ \]
125
126  print -R $A[$o] $A[$s[(r)\[]]
127  print -R $A[(r)$c] $A[(r)$s[(r)\]]]
128  print -R $A[$A[(i)\\\\\]]]
1290:Associative array lookup using a pattern subscript to get the key
130>obrack obrack
131>] ]
132>backcbrack
133
134  print -R ${A[${A[(r)\\\\\\\\\]]}]::=zounds}
135  print -R ${A[${A[(r)\\\\\\\\\]]}]}
136  print -R $A[\\\\\]]
1370:Associative array substitution-assignment with reverse pattern subscript key
138>zounds
139>zounds
140>zounds
141
142  print -R ${(o)A[(K)\]]}
143  print -R ${(o)A[(K)\\\]]}
1440:Associative array keys interpreted as patterns
145>\2 backcbrack cbrack star
146>\\\4 \\\? star zounds
147
148# It doesn't matter which element we get, since we never guarantee
149# ordering of an associative array.  So just test the number of matches.
150  array=(${(o)A[(k)\]]})
151  print ${#array}
152  array=(${(o)A[(k)\\\]]})
153  print ${#array}
1540:Associative array keys interpreted as patterns, single match
155>1
156>1
157
158  typeset -g "A[one\"two\"three\"quotes]"=QQQ
159  typeset -g 'A[one\"two\"three\"quotes]'=qqq
160  print -R "$A[one\"two\"three\"quotes]"
161  print -R $A[one\"two\"three\"quotes]
162  A[one"two"three"four"quotes]=QqQq
163  print -R $A[one"two"three"four"quotes]
164  print -R $A[$A[(i)one\"two\"three\"quotes]]
165  print -R "$A[$A[(i)one\"two\"three\"quotes]]"
1660:Associative array keys with double quotes
167>QQQ
168>qqq
169>QqQq
170>qqq
171>QQQ
172
173  print ${x::=$A[$A[(i)one\"two\"three\"quotes]]}
174  print $x
175  print ${x::="$A[$A[(i)one\"two\"three\"quotes]]"}
176  print $x
1770:More keys with double quotes, used in assignment-expansion
178>qqq
179>qqq
180>QQQ
181>QQQ
182
183  qqq=lower
184  QQQ=upper
185  print ${(P)A[one\"two\"three\"quotes]}
186  print "${(P)A[$A[(i)one\"two\"three\"quotes]]}"
1870:Keys with double quotes and the (P) expansion flag
188>lower
189>upper
190
191  typeset -ga empty
192  echo X${${empty##*}[-1]}X
1930:Negative index applied to substition result from empty array
194>XX
195
196  print $empty[(i)] $empty[(I)]
1970:(i) returns 1 for empty array, (I) returns 0.
198>1 0
199
200  array=(one two three four)
201  print X$array[0]X
2020:Element zero is empty if KSH_ZERO_SUBSCRIPT is off.
203>XX
204
205  array[0]=fumble
2061:Can't set element zero if KSH_ZERO_SUBSCRIPT is off.
207?(eval):1: array: assignment to invalid subscript range
208
209  print X$array[(R)notfound]X
2100:(R) returns empty if not found if KSH_ZERO_SUBSCRIPT is off.
211>XX
212
213  setopt KSH_ZERO_SUBSCRIPT
214  print X$array[0]X
2150:Element zero is element one if KSH_ZERO_SUBSCRIPT is on.
216>XoneX
217
218  array[0]=fimble
219  print $array
2200:Can set element zero if KSH_ZERO_SUBSCRIPT is on.
221>fimble two three four
222
223  print X$array[(R)notfound]X
2240:(R) yuckily returns the first element on failure withe KSH_ZERO_SUBSCRIPT
225>XfimbleX
226
227  unsetopt KSH_ZERO_SUBSCRIPT
228  array[(R)notfound,(r)notfound]=(help help here come the seventies retreads)
229  print $array
2300:[(R)notfound,(r)notfound] replaces the whole array
231>help help here come the seventies retreads
232
233  string="Why, if it isn't Officer Dibble"
234  print "[${string[0]}][${string[1]}][${string[0,3]}]"
2350:String subscripts with KSH_ZERO_SUBSCRIPT unset
236>[][W][Why]
237
238  setopt KSH_ZERO_SUBSCRIPT
239  print "[${string[0]}][${string[1]}][${string[0,3]}]"
2400:String subscripts with KSH_ZERO_SUBSCRIPT set
241>[W][W][Why]
242
243  unsetopt KSH_ZERO_SUBSCRIPT
244  string[0,3]="Goodness"
245  print $string
2460:Assignment to chunk of string ignores element 0
247>Goodness, if it isn't Officer Dibble
248
249  string[0]=!
2501:Can't set only element zero of string
251?(eval):1: string: assignment to invalid subscript range
252