-
Notifications
You must be signed in to change notification settings - Fork 3
/
arrayutils.bash
268 lines (246 loc) · 6.81 KB
/
arrayutils.bash
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
#!/usr/bin/env bash
#
# File:
# arrayutils.bash
#
# Description:
# Bash array utilities
#
# ============================================
# Validation
# ============================================
# Usage:
# arrayContainsValue <value> <array reference>
arrayContainsValue() {
local arr="${2}[@]"
arr=("${!arr}")
for arrVal in "${arr[@]}"; do [ "${1}" = "${arrVal}" ] && \
return 0; done
return 1
}
# Usage:
# arrayContainsKey <key> <array reference>
arrayContainsKey() {
local arr="${2}[@]"
arr=("${!arr}")
for arrKey in "${!arr[@]}"; do [ "${1}" = "${arrKey}" ] && \
return 0; done
return 1
}
# Check if a value matches an array regex value.
# Usage:
# arrayContainsValueByRegex <regex> <array reference>
arrayContainsValueByRegex() {
local arr="${2}[@]"
arr=("${!arr}")
for arrVal in "${arr[@]}"; do [[ "${arrVal}" =~ ${1} ]] && \
return 0; done
return 1
}
# Check if array contains a value that is a prefix for the given value.
# Usage:
# arrayContainsPrefixForValue <value> <array reference>
arrayContainsPrefixForValue() {
local arr="${2}[@]"
arr=("${!arr}")
for arrVal in "${arr[@]}"; do [[ "${1}" = "${arrVal}"* ]] && \
return 0; done
return 1
}
# ============================================
# Getters
# ============================================
# Get a value from an array
# Usage:
# getArrayValue <value> <array reference>
getArrayValue() {
local arr="${2}[@]"
arr=("${!arr}")
for arrVal in "${arr[@]}"; do [ "${arrVal}" = "${1}" ] && \
echo "${arrVal}" && break; done
}
# Get the position of an array element matching a value.
# Usage:
# getPositionOfArrayElement <value> <array reference>
getPositionOfArrayElement() {
local arr="${2}[@]"
arr=("${!arr}")
for indx in ${!arr[@]}; do [ "${arr[${indx}]}" = "${1}" ] && \
echo "${indx}" && break; done
}
# Get the first value in the array that contains the given string.
# Usage:
# getFirstArrayValueContainingString <string> <array reference>
getFirstArrayValueContainingString() {
local arr="${2}[@]"
arr=("${!arr}")
for arrVal in "${arr[@]}"; do [[ "${1}" = *"${arrVal}"* ]] && \
echo "${arrVal}" && break; done
}
# ============================================
# Manipulation
# ============================================
# Usage:
# removeElementFromArray <value> <array reference>
removeElementFromArray() {
local arr="${2}[@]"
arr=("${!arr}")
local indx=0
for arrVal in "${arr[@]}"; do
[ "${arrVal}" = "${1}" ] && \
arr=("${arr[@]:0:${indx}} ${arr[@]:$((${indx} + 1))}") && \
break
((indx++))
done
echo "${arr[@]}"
}
# Usage:
# removeDuplicatesFromArray <array reference>
removeDuplicatesFromArray() {
local arr="${1}[@]"
arr=("${!arr}")
local retArr
for arrVal in "${arr[@]}"; do
local has=0
for retArrVal in "${retArr[@]}"; do
[ "${retArrVal}" = "${arrVal}" ] && has=1 && break; done
[ ${has} -eq 0 ] && retArr+=("${arrVal}")
done
echo "${retArr[@]}"
}
# ============================================
# Sets
# ============================================
# Get the intersection of two arrays.
# Example:
# A=(1 3 9 2 7); B=(2 5 3 7 5)
# getIntersection A B # returns "3 2 7"
getIntersection() {
local arr1="${1}[@]"
local arr2="${2}[@]"
arr1=(${!arr1})
arr2=(${!arr2})
local retArr
for arr1Val in "${arr1[@]}"; do
for arr2Val in "${arr2[@]}"; do
if [ "${arr2Val}" = "${arr1Val}" ]; then
local has=0
for retArrVal in "${retArr[@]}"; do
[ "${retArrVal}" = "${arr1Val}" ] && has=1 && break; done
[ ${has} -eq 0 ] && retArr+=("${arr1Val}")
fi
done
done
echo "${retArr[@]}"
}
# Get the union of two arrays.
# Example:
# A=(5 7 1 1 4); B=(3 1 4 1 9)
# getUnion A B # returns "5 7 1 4 3 9"
getUnion() {
local arr1="${1}[@]"
local arr2="${2}[@]"
arr1=(${!arr1})
arr2=(${!arr2})
local retArr
for arr1Val in "${arr1[@]}"; do
local has=0
for retArrVal in "${retArr[@]}"; do
[ "${retArrVal}" = "${arr1Val}" ] && has=1 && break; done
[ ${has} -eq 0 ] && retArr+=("${arr1Val}")
done
for arr2Val in "${arr2[@]}"; do
local has=0
for retArrVal in "${retArr[@]}"; do
[ "${retArrVal}" = "${arr2Val}" ] && has=1 && break; done
[ ${has} -eq 0 ] && retArr+=("${arr2Val}")
done
echo "${retArr[@]}"
}
# Get the union of two arrays (2).
# Example:
# A=(5 7 1 1 4); B=(3 1 4 1 9)
# getUnion2 A B # returns "5 7 3 1 4 9"
getUnion2() {
local arr1="${1}[@]"
local arr2="${2}[@]"
arr1=(${!arr1})
arr2=(${!arr2})
local unionArr
local i=0 j=0
while [[ ${i} -lt ${#arr1[@]} && ${j} -lt ${#arr2[@]} ]]; do
if [[ "${arr1[${i}]}" > "${arr2[${j}]}" ]]; then
unionArr+=("${arr1[$((i++))]}")
elif [[ "${arr1[${i}]}" < "${arr2[${j}]}" ]]; then
unionArr+=("${arr2[$((j++))]}")
else
unionArr+=("${arr1[$((i++))]}")
((j++))
fi
done
while [ ${i} -lt ${#arr1[@]} ]; do unionArr+=("${arr1[$((i++))]}"); done
while [ ${j} -lt ${#arr2[@]} ]; do unionArr+=("${arr2[$((j++))]}"); done
local unionNoDuplctsArr
for unionArrVal in "${unionArr[@]}"; do
local has=0
for unionNoDuplctsArrVal in "${unionNoDuplctsArr[@]}"; do
[ "${unionNoDuplctsArrVal}" = "${unionArrVal}" ] && has=1 && break
done
[ ${has} -eq 0 ] && unionNoDuplctsArr+=("${unionArrVal}")
done
echo "${unionNoDuplctsArr[@]}"
}
# Get the difference between array 1 and array 2 (i.e. elements in array 1
# that are not in array 2).
# Example:
# A=(3 6 2 1 5 1 1); B=(2 4 6)
# getDifference A B # returns "3 1 5"
getDifference() {
local arr1="${1}[@]"
local arr2="${2}[@]"
arr1=(${!arr1})
arr2=(${!arr2})
local retArr
for arr1Val in "${arr1[@]}"; do
local has=0
for retArrVal in "${retArr[@]}"; do
[ "${retArrVal}" = "${arr1Val}" ] && has=1 && break; done
[ ${has} -eq 0 ] && for arr2Val in "${arr2[@]}"; do
[ "${arr2Val}" = "${arr1Val}" ] && has=1 && break; done
[ ${has} -eq 0 ] && retArr+=("${arr1Val}")
done
echo "${retArr[@]}"
}
# ============================================
# Parsing
# ============================================
# Join the elements of an array by a delimiter.
# Usage:
# joinArrayBy <delimiter> <array reference>
joinArrayBy() {
local delim="${1}"
local arr="${2}[@]"
arr=("${!arr}")
joined="$(printf "%s" "${arr[@]/#/$delim}")"
echo "${joined#${delim}}"
}
# ============================================
# Other
# ============================================
# Create pairs between array 1 and array 2.
# Example:
# A=(0 12); B=(4 5)
# createPairs A B # returns "0:4 0:5 12:4 12:5"
createPairs() {
local arr1="${1}[@]"
local arr2="${2}[@]"
arr1=(${!arr1})
arr2=(${!arr2})
local retArr
for arr1Val in ${arr1[@]}; do
for arr2Val in ${arr2[@]}; do
local retArr+=("${arr1Val}:${arr2Val}")
done
done
echo "${retArr[@]}"
}