From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: zsh-users@zsh.org
Subject: Re: shuffle array
Date: Mon, 2 Dec 2019 14:14:21 +0000 [thread overview]
Message-ID: <20191202141421.sj7nlhsdrpqxpr42@chaz.gmail.com> (raw)
In-Reply-To: <20191201233848.7selcpkvenlu65px__33142.5388505281$1575243619$gmane$org@tarpaulin.shahaf.local2>
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
next prev parent reply other threads:[~2019-12-02 14:15 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-29 3:20 Emanuel Berg
2019-11-29 6:42 ` Marc Chantreux
2019-11-29 8:19 ` Emanuel Berg
2019-11-29 8:43 ` Marc Chantreux
2019-11-29 8:50 ` Emanuel Berg
2019-11-29 13:38 ` Clint Adams
[not found] ` <8636e7w0w9.fsf__36104.0529723809$1575015640$gmane$org@zoho.eu>
2019-12-01 20:07 ` Stephane Chazelas
2019-12-01 21:22 ` Emanuel Berg
2019-12-01 21:25 ` Emanuel Berg
2019-12-01 23:46 ` Daniel Shahaf
2019-12-02 4:10 ` Emanuel Berg
2019-12-01 23:38 ` Daniel Shahaf
[not found] ` <20191201233848.7selcpkvenlu65px__33142.5388505281$1575243619$gmane$org@tarpaulin.shahaf.local2>
2019-12-02 14:14 ` Stephane Chazelas [this message]
2019-12-02 14:29 ` Roman Perepelitsa
2019-11-29 8:44 ` Emanuel Berg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191202141421.sj7nlhsdrpqxpr42@chaz.gmail.com \
--to=stephane.chazelas@gmail.com \
--cc=d.s@daniel.shahaf.name \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).