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

Re: shuffle array



Stephane Chazelas wrote:

> You could do things like:
>
> array=("${(0@)$(printf '%s\0' "$array[@]" | shuf -z)[1,-2]}")

It seems I always get the same shuffle
for that?

(1 2 3 a b c) -> (c 1 a 3 b 2)

and

(x y z 1 2 3) -> (3 x 1 z 2 y)

- the same.

Another thing, how do I use this to shuffle an
array from another function? How do I send the
array as a name/pointer argument, i.e. not just
the value? Or how can I return the new array
and set the old one's value to that?

# from SC on gmane.comp.shells.zsh.user
shuffle-array-2 () {
    local -a array
    array=($@)
    array=("${(0@)$(printf '%s\0' "$array[@]" | shuf -z)[1,-2]}")
    echo $array
}

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

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



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