Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: shuffle array



This seems to work, I got help on
#zsh@xxxxxxxxxxxxxxxx, then rewrote it a bit as
I'm not familiar with the 'typeset' and
'integer' commands, also the parenthesis syntax
is unknown to me... but anyway, source at

    https://dataswamp.org/~incal/conf/.zsh/misc

Relevant part yanked:

# from: http://mika.l3ib.org/code/zsh-functions/shufflearray
swap-array () {
    zmodload zsh/mathfunc

    local -a reply
    reply=($@)

    local -a swap

    local n=$#reply
    local k

    for ((; n > 0; n-- )); do
        k=$((1 + int(rand48()*n)))
        swap=$reply[k]
        reply[k]=$reply[n]
        reply[n]=$swap
    done

    echo $reply
}

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal



Messages sorted by: Reverse Date, Date, Thread, Author