Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: shuffle array
2019-12-01 23:38:48 +0000, Daniel Shahaf:
[...]
> I think the following would work? —
>
> typeset -a l=(…)
> eval "l=(" $(print -rl -- "${(@qqqq)l}" | shuf) ")"
>
> shuf(1) expects newline-separated output, and the output of ${(qqqq)} never
> has literal newlines.
$(...) should be quoted or there'll be IFS-splitting.
I wouldn't trust qqqq with eval in locales using charsets like
BIG5, BIG5-HKSCS, GB18030...
$ LC_ALL=zh_HK.big5hkscs luit
$ l=('α')
$ eval "print -r -- $(printf '%s\n' "${(@qqqq)l}")"
zsh: unmatched '
In pratice, I think only ${(q)...} (using single quotes) is
totally safe for reinput regardless of the locale (at least with
the locales typically available on GNU systems).
So you'd want to do something like:
(){ local LC_ALL=C
eval "l=($(printf '%s\n' "${(@qqqq)l}" | shuf))"
}
That "eval" would still make me nervous.
The:
l=(/(Ne['reply=("$l[@]")']oe['REPLY=$RANDOM']))
approach I gave would not have this kind of issue and would
avoid the dependency on a GNU utility. (it's got its own issues
linked to zsh's $RANDOM taking only 32768 different values, or
being easy to guess...).
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author