I need to delete specific values in an array (which vary in their index positions), similar to the splice function in javascript.
Example:
set -A ARRAY1 "a" "b" "c" "d" "e"
set -A ARRAY2 "a" "c" "e"
# Expected ARRAY1 after splice: "b" "d"
What I've tried:
I iterated through the arrays to find matches to the values I want deleted, and set them to empty ("").
ITERATION=0
for i in "${ARRAY1[@]}"
do
for j in "${ARRAY2[@]}"
do
if [[ $i == $j ]]
then
ARRAY1[$ITERATION]=""
fi
done
ITERATION=$((ITERATION+1))
done
#ARRAY1 after emptying values: "" "b" "" "d" ""
After that, I made a variable to store the concatenation of the first array's values.
VARIABLE=${ARRAY1[@]}
Then set the array back together again.
set -A ARRAY1 $VARIABLE
# VARIABLE: b d
Now the ARRAY1 has 2 indexes with values "b" and "d" as expected.
echo "ARRAY1: ${ARRAY1[@]}"
# output: ARRAY1: b d
I tried searching for the correct way to do this but couldn't find anything, and I think my solution is not right, even if it seems to work. Is there a correct or better way to do this? Is there a function for this in ksh?
Thanks in advance!