about summary refs log tree commit diff
path: root/Test/D06subscript.ztst
blob: ac233c1a2c6aabb472f896bf6735f3e84693c275 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Test parameter subscripting.

%prep

  s='Twinkle, twinkle, little *, [how] I [wonder] what?  You are!'
  a=('1' ']'  '?' '\2'  '\]' '\?'  '\\3' '\\]'  '\\?' '\\\4'  '\\\]' '\\\?')
  typeset -g -A A
  A=($a)

%test

  x=','
  print $s[(i)winkle] $s[(I)winkle]
  print ${s[(i)You are]} $#s
  print ${s[(r)$x,(R)$x]}
0:Scalar pattern subscripts without wildcards
>2 11
>53 60
>, twinkle, little *,

  x='*'
  print $s[(i)*] $s[(i)\*] $s[(i)$x*] $s[(i)${(q)x}*] $s[(I)$x\*]
  print $s[(r)?,(R)\?] $s[(r)\?,(R)?]
  print $s[(r)\*,(R)*]
  print $s[(r)\],(R)\[]
0:Scalar pattern subscripts with wildcards
>1 26 1 26 26
>Twinkle, twinkle, little *, [how] I [wonder] what? ?  You are!
>*, [how] I [wonder] what?  You are!
>] I [

  # $s[(R)x] actually is $s[0], but zsh treats 0 as 1 for subscripting.
  print $s[(i)x] : $s[(I)x]
  print $s[(r)x] : $s[(R)x]
0:Scalar pattern subscripts that do not match
>61 : 0
>: T

  print -R $s[$s[(i)\[]] $s[(i)$s[(r)\*]] $s[(i)${(q)s[(r)\]]}]
0:Scalar subscripting using a pattern subscript to get the index
>[ 1 33

  print -R $a[(r)?] $a[(R)?]
  print $a[(n:2:i)?] $a[(n:2:I)?]
  print $a[(i)\?] $a[(I)\?]
  print $a[(i)*] $a[(i)\*]
0:Array pattern subscripts
>1 ?
>2 2
>3 3
>1 13

  # It'd be nice to do some of the following with (r), but we run into
  # limitations of the ztst script parsing of backslashes in the output.
  print -R $a[(i)\\\\?] $a[(i)\\\\\?]
  print -R $a[(i)\\\\\\\\?] $a[(i)\\\\\\\\\?]
  print -R ${a[(i)\\\\\\\\?]} ${a[(i)\\\\\\\\\?]}
  print -R "$a[(i)\\\\\\\\?] $a[(i)\\\\\\\\\?]"
  print -R $a[(i)\]] $a[(i)\\\\\]] $a[(i)\\\\\\\\\]] $a[(i)\\\\\\\\\\\\\]]
  print -R $a[(i)${(q)a[5]}] $a[(i)${(q)a[8]}] $a[(i)${(q)a[11]}]
  print -R $a[(i)${a[3]}] $a[(i)${a[6]}] $a[(i)${a[9]}] $a[(i)${a[12]}]
0:Array pattern subscripts with multiple backslashes
>4 6
>7 9
>7 9
>7 9
>2 5 8 11
>5 8 11
>1 3 4 6

  print -R $A[1] $A[?] $A[\\\\3] $A[\\\]]
  print -R $A[$a[11]]
  print -R $A[${(q)a[5]}]
0:Associative array lookup (direct subscripting)
>] \2 \\] \?
>\\\?
>\\\?

  # The (o) is necessary here for predictable output ordering
  print -R $A[(I)\?] ${(o)A[(I)?]}
  print -R $A[(i)\\\\\\\\3]
  print -R $A[(I)\\\\\\\\\?] ${(o)A[(I)\\\\\\\\?]}
0:Associative array lookup (pattern subscripting)
>? 1 ?
>\\3
>\\? \\3 \\?

  print -R $A[(R)\?] : ${(o)A[(R)?]}
  print -R $A[(R)\\\\\?] ${(o)A[(R)\\\\?]} ${(o)A[(R)\\\\\?]}
  print -R ${(o)A[(R)\\\\\\\\\]]}
0:Associative array lookup (reverse subscripting)
>: ]
>\? \2 \? \?
>\\]

  eval 'A[*]=star'
1:Illegal associative array assignment
?ZTST_execchunk:1: A: attempt to set slice of associative array

  x='*'
  A[$x]=xstar
  A[${(q)x}]=qxstar
  print -R ${(k)A[(r)xstar]} $A[$x]
  print -R ${(k)A[(r)qxstar]} $A[${(q)x}]
  A[(e)*]=star
  A[\*]=backstar
  print -R ${(k)A[(r)star]} $A[(e)*]
  print -R ${(k)A[(r)backstar]} $A[\*]
0:Associative array assignment
>* xstar
>\* qxstar
>* star
>\* backstar

  o='['
  c=']'
  A[\]]=cbrack
  A[\[]=obrack
  A[\\\[]=backobrack
  A[\\\]]=backcbrack
  print -R $A[$o] $A[$c] $A[\[] $A[\]] $A[\\\[] $A[\\\]]
  print -R $A[(i)\[] $A[(i)\]] $A[(i)\\\\\[] $A[(i)\\\\\]]
0:Associative array keys with open and close brackets
>obrack cbrack obrack cbrack backobrack backcbrack
>[ ] \[ \]

  print -R $A[$o] $A[$s[(r)\[]]
  print -R $A[(r)$c] $A[(r)$s[(r)\]]]
  print -R $A[$A[(i)\\\\\]]]
0:Associative array lookup using a pattern subscript to get the key
>obrack obrack
>] ]
>backcbrack

  print -R ${A[${A[(r)\\\\\\\\\]]}]::=zounds}
  print -R ${A[${A[(r)\\\\\\\\\]]}]}
  print -R $A[\\\\\]]
0:Associative array substitution-assignment with reverse pattern subscript key
>zounds
>zounds
>zounds

  print -R ${(o)A[(K)\]]}
  print -R ${(o)A[(K)\\\]]}
0:Associative array keys interpreted as patterns
>\2 backcbrack cbrack star
>\\\4 \\\? star zounds

  typeset "A[one\"two\"three\"quotes]"=QQQ
  typeset 'A[one\"two\"three\"quotes]'=qqq
  print -R "$A[one\"two\"three\"quotes]"
  print -R $A[one\"two\"three\"quotes]
  A[one"two"three"four"quotes]=QqQq
  print -R $A[one"two"three"four"quotes]
  print -R $A[$A[(i)one\"two\"three\"quotes]]
  print -R "$A[$A[(i)one\"two\"three\"quotes]]"
0:Associative array keys with double quotes
>QQQ
>qqq
>QqQq
>qqq
>QQQ

  print ${x::=$A[$A[(i)one\"two\"three\"quotes]]}
  print $x
  print ${x::="$A[$A[(i)one\"two\"three\"quotes]]"}
  print $x
0:More keys with double quotes, used in assignment-expansion
>qqq
>qqq
>QQQ
>QQQ

  qqq=lower
  QQQ=upper
  print ${(P)A[one\"two\"three\"quotes]}
  print "${(P)A[$A[(i)one\"two\"three\"quotes]]}"
0:Keys with double quotes and the (P) expansion flag
>lower
>upper