bash手册中的一段内容。
The unset builtin is used to destroy arrays. unset name[subscript]
destroys the array element at index subscript. Care must be taken to
avoid unwanted side effects caused by pathname expansion. unset name,
where name is an array, or unset name[subscript], where subscript is *
or @, removes the entire array.
所以,unset会销毁数组元素。在内存中,这可能只是指针移动。
更新: 看看这个。由于未删除引用并以一种方式更新数组长度,因此似乎缺少$RANDOM始终返回介于0和当前长度之间的数字。
Length of array: 8
8 random number 7 -> purple
Length of array: 7
7 random number 6 -> blue
Length of array: 6
6 random number 2 -> black
Length of array: 5
5 random number 2 ->
Length of array: 5
5 random number 0 -> red
一种可能的解决方案是覆盖数组。
更新:仅当您取消设置尚未取消设置的数组元素时,长度才会更改并保留初始长度。如果您删除元素1,则会进入无限循环,然后长度为7,但是您将无法再产生随机数8。因此,长度将至少为1。
您需要始终在0和8之间生成随机数。此外,您可以存储已删除的索引。
length=${#a[@]} # ADD
while [ ${#a[@]} -ne 0 ]
do
echo "Length of array:" ${#a[@]}
randomnumber=$(( $RANDOM % ${#a[@]} )) # if using this arrange the index afterwards like Cyrus said, avoids the overhead mentioned below
#randomnumber=$(( $RANDOM % $length )) # regard initial length
echo "random number "$randomnumber" -> "${a[$randomnumber]}
unset a[$randomnumber]
a=(${a[@]}) # rearranges the indexes
sleep 2
done
至少在不考虑已经被删除的索引的情况下,选择/创建已经被取消的索引的随机数会增加一些开销。如果$RANDOM生成功能不正常并且没有返回范围内的数字,则仍然可能出现无限循环。此行为随着数组长度的增加而增加。
无论如何,这是一个很棒的问题 :)