Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility functions for array manipulation #6203

Merged
merged 4 commits into from
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions qa/common/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,38 @@ function kill_servers () {
function collect_artifacts_from_subdir () {
cp *.*log* core* ../ || true
}

# Sort an array
# Call with sort_array <array_name>
# Example: remove_array_outliers array
sort_array() {
local -n arr=$1
local length=${#arr[@]}

if [ "$length" -le 1 ]; then
return
fi

IFS=$'\n' sorted_arr=($(sort -n <<<"${arr[*]}"))
unset IFS
arr=("${sorted_arr[@]}")
}

# Remove an array's outliers
# Call with remove_array_outliers <array_name> <percent to trim from both sides>
# Example: remove_array_outliers array 5
remove_array_outliers() {
local -n arr=$1
local percent=$2
local length=${#arr[@]}

if [ "$length" -le 1 ]; then
return
fi

local trim_count=$((length * percent / 100))
local start_index=$trim_count
local end_index=$((length - (trim_count*2)))

arr=("${arr[@]:$start_index:$end_index}")
}