Hi,

I run into issue trying to populate an array with unique random value, the issue is that I have an endless loop, here the snippet:

...
# Populate playlist array with unique random values
while [ "$index" -le "$total" ]; do
        clip="$(/usr/bin/numrandom /1..$total/)"
if [[ ! ${playlist[@]} =~ "$clip" ]]; then # [ -z ${rndarr[clip]} ]; then
                                                        # rndarr[$clip]=1
               playlist[$index]=$clip
               ((index++))
        fi
done
...

It works fine if I use the second array "rndarr[]". I use that array to store 1 at the rndarr[$index] occurrence then, after incremented the index, I check whether it's empty (no 1 stored) at the new $index value.

My wishes is to avoid to use a second array (rndarr[]) and use playlist[] array only to do this, but with the above code I have an endless loop, why?

I checked the remarkable ABS ¹ handbook at chapter 27 but nothing helped about finding values inside an array (only replacement examples).

Thanks in advance for any hint, kind regards,

¹ https://tldp.org/LDP/abs/abs-guide.pdf
--
Franco Martelli

Reply via email to