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

Re: shuffle array



2019-11-29 09:19:02 +0100, Emanuel Berg:
> Marc Chantreux wrote:
> 
> > print -l $array |shuff|read -A array
> 
> *shuf
> 
> The first two steps work

The first two steps (with shuf instead of shuff)  only work if
array elements don't contain backslashes nor newline and are not
empty and the first one doesn't start with "-".

> but the result array
> seems to have only one element?
[...]

Yes, that's not what read -A does.

You could do things like:

array=("${(0@)$(printf '%s\0' "$array[@]" | shuf -z)[1,-2]}")

But that would still not work for array elements containing
NULs.

-- 
Stephane



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