-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arrays.ahk
327 lines (298 loc) · 8.4 KB
/
Arrays.ahk
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
Extended library for Arrays
(c) 2022-2024 Ken Verdadero
2022-06-02
*/
/**
* Displays the array as string (like in Python)
*/
ArrayAsStr(arr, delims := ", ", end := "", brackets := true, unit := "", unitEnd := true, border := '') {
if Type(arr) != "Array"
throw TypeError("Expected an Array type, got " Type(arr), , arr)
o := (brackets ? "[" : "")
for i in arr {
if !InStr("String Integer Float", Type(i))
i := '<' Type(i) '>'
try o .= border (!unitEnd ? unit : '') i (unitEnd ? unit : "") border (A_Index == arr.Length ? end : delims)
catch Error {
throw Error("Push error in " Type(arr) " type element at index " A_Index)
}
}
o .= (brackets ? ']' : '')
return o
}
/**
* Returns the length of the array
* @param Arr The array to be measured
* @param {Integer} Reversed If true, the output will be reversed
* @returns {Array} Returns the length of the array
*/
ArraySort(Arr, Reversed := false) {
ArrStr := ''
if !IsObject(Arr) {
return
}
if Reversed {
_r := []
while (i := Arr.Length - A_Index + 1) {
_r.Push(Arr[i])
}
Arr := _r
}
for e in Arr {
ArrStr .= e "`n"
}
ArrStr := StrSplit(Sort(Trim(ArrStr, "`n"), (Reversed ? "R" : "")), "`n")
return ArrStr
}
/* Sorts the array but with numbers */
ArraySortNum(arr, reversed := false) {
if !reversed {
out := []
rev := ArraySortNum(arr, true)
for i, n in rev {
out.Push(rev[rev.Length - i + 1])
}
return out
}
out := []
last_idx := 1
while arr.Length {
last := 0
for i, n in arr {
if n > last {
last := n
last_idx := i
}
}
try arr.RemoveAt(last_idx)
catch Error {
; MsgBox(ArrayAsStr(arr) ' & ' ArrayAsStr(out))
arr.RemoveAt(-1)
}
out.Push(last)
}
return out
}
/* Returns the sum of all elements in an array */
ArraySum(arr) {
o := 0
for i in arr {
(isDigit(String(i)) ? (o += i) : 0)
}
return o
}
ArraySubtract(minuend, subtrahend) {
if Type(minuend) != "Array" && Type(subtrahend) != "Array"
throw TypeError("Expected an Array type, got " type(minuend))
o := []
for i in minuend {
if !ArrayMatch(i, subtrahend)
o.Push(i)
}
return o
}
/* Returns the average of elements inside the array */
ArrayAvg(arr, remainder := -1) {
if Type(arr) != "Array"
throw TypeError("Expected an Array type, got " type(arr))
try out := ArraySum(arr) / arr.Length
catch Error {
out := 0 ;; When length is zero.
}
return (remainder != -1 ? Round(out, remainder) : out)
}
/* Returns true if the needles matches any element in an array */
ArrayMatch(needle, haystack, caseSensitive := false) {
for i in haystack {
if (caseSensitive ? (i == needle) : (i = needle)) {
return 1
}
}
return 0
}
/* Generates an array from range of numbers
Also accepts string insertion along with numbers.
- When strAtEnd is true, the string will be inserted at the end of number
*/
ArrayRange(start, end, str := "", strAtEnd := false) {
o := []
while start < end + 1 {
o.Push(
(!strAtEnd ? str : "")
start
(strAtEnd ? str : "")
)
start++
}
return o
}
/* Merges array into a single array */
ArrayMerge(arr*) {
o := []
for a in arr { ;; Loop through every arg (assumed they're all ArrayType)
if Type(a) != "Array" { ;; Ignore not array type in variadic EXCEPT for string—
if Type(a) != "String" ;; as String can be converted to array
continue
a := Array(a)
}
for i in a { ;; Push every value to a new array object (o)
o.Push(i)
}
}
return o
}
/**
* Converts the string into array of characters.
*/
ArrayFromStr(String, Delimiters?, OmitChars?, MaxParts := -1) => StrSplit(String, Delimiters, OmitChars, MaxParts)
/* Subtracts Array A with Array B */
ArrayDiff(arrA, arrB) {
if Type(arrA) != "Array" && Type(arrB) != "Array"
throw TypeError("Expected an Array type, got " type(arrA))
o := []
for i in arrA {
(!ArrayMatch(i, arrB) ? o.push(i) : 0)
}
return o
}
/* Merge arrays but discards duplicate elements */
ArrayUnique(arrays*) {
m := []
for i in arrays {
for j in i {
m.Push(j)
}
}
m := ArrayMerge(m)
o := []
for i in m {
if !ArrayMatch(i, o)
o.Push(i)
}
return o
}
/**
* Returns matched elements in an array.
* This method uses regular expression in which it matches all elements
* that contains the keyword regardless of its position and case-sensitivity.
*
* Default match mode is '`Contains`' which matches all elements that contains
* the keyword.
*
* Improvements:
* - Have an option change match mode (ongoing 2022-06-05)
*
* @param {Array} haystack The array to be filtered
* @param {String} needle The keyword to be matched
* @param {String} matchMode The match mode to be used. Default is '`Contains`'
* @returns {Array} Returns an array of matched elements
*/
ArrayFilter(haystack, needle, matchMode := 'Contains', caseSensitive := false) {
FILTERED := []
caseSensitive := (caseSensitive ? "" : "(?i)")
for e in haystack {
try {
switch StrLower(matchMode) {
case 'contains':
if RegExMatch(e, Format("{1}({2})", caseSensitive, needle)) {
FILTERED.push(e)
}
case 'exact':
if RegExMatch(e, Format("^{1}({2}$)", caseSensitive, needle)) {
FILTERED.push(e)
}
case 'startswith':
if RegExMatch(e, Format("^{1}({2})", caseSensitive, needle)) {
FILTERED.push(e)
}
}
} catch Error {
return []
}
}
return FILTERED
}
/**
* Finds the index number of the needle element in haystack.
* Throws ValueError if not found.
*/
ArrayFind(haystack, needle, caseSensitive := false) {
if Type(haystack) != "Array" {
throw TypeError("Parameter #1 is not an Array type. Got " Type(haystack))
}
if caseSensitive {
for i, e in haystack {
if e == needle {
return i
}
}
} else {
for i, e in haystack {
if e = needle {
return i
}
}
}
throw ValueError(Format("'{1}' cannot be found in the array", needle))
}
/* Similar to SubStr but applies to array objects */
ArrayTrunc(arr, startPos := 1, elements := -1) {
o := []
e := 0
startPos := (startPos < 0 ? arr.Length + startPos + 1 : startPos)
for i in arr {
if A_Index >= startPos &&
(elements > 0 ? e < elements : e < arr.Length + elements + 1) { ;; Pushes the element if it's in scope of startPos and element number is in range
o.Push(i)
e++
}
}
return o
}
/* Returns the index of the highest value in an array */
ArrayMaxIndex(arr) {
HIGH := 0
IDX := 0
for i in arr {
if i < HIGH && A_Index > 1
continue
HIGH := i
IDX := A_Index
}
return IDX
}
/* Returns the index of the lowest value in an array */
ArrayMinIndex(arr) {
LOW := 0
IDX := 0
for i in arr {
if i > LOW && A_Index > 1
continue
LOW := i
IDX := A_Index
}
return IDX
}
/* Returns the lowest value in an array */
ArrayMin(arr) {
try arr[ArrayMinIndex(arr)]
catch Error {
return 0
}
}
/* Returns the highest value in an array */
ArrayMax(arr) {
try arr[ArrayMaxIndex(arr)]
catch Error {
return 0
}
}
/* Reverses an array */
ArrayReverse(arr) {
o := []
for i, e in arr {
o.Push(arr[-i])
}
return o
}