From: Sebastian Gniazdowski <psprint@zdharma.org>
To: zsh-workers@zsh.org
Subject: Documentation for code execution in substitutions using patterns
Date: Tue, 17 Oct 2017 09:01:17 +0200 [thread overview]
Message-ID: <etPan.59e5aabd.37236a33.98a8@zdharma.org> (raw)
Hello,
Zsh has very flexible substitutions. So flexible that I was able to write ZUI library, and if I was to port it to e.g. python, it would be probably hard if not impossible. But I think the most powerful part – code execution – isn't documented and isn't always active. For example:
% local -A map=( a 1 b 2 ); str="abc"; echo "${str//(#m)?/${map[$MATCH]}}"
12
% unsetopt extendedglob
% local -A map=( a 1 b 2 ); str="abc"; echo "${str//(#m)?/${map[$MATCH]}}"
abc
If (#m) alone (without e-glob) could do code execution then a more minimalistic code could be written (no need for localoptions extendedglob). Other example:
% line=0; arr=( a b c d ); echo ${arr[@]//(#b)(*)/$(( ++line ))}
1 2 3 4
% line=0; arr=( a b c d ); echo ${arr[@]//(#b)*/$(( ++ line ))}
1 1 1 1
(#b) needs extendedglob so no additional variations here. One more less-meaningful substitution:
% line=0; arr=( a b c d ); echo ${arr[@]//(*)/$(( ++ line ))}
1 1 1 1
Maybe there's something more on this topic? These nuances could be documented after explaining why above variations exist?
--
Sebastian Gniazdowski
psprint /at/ zdharma.org
reply other threads:[~2017-10-17 7:01 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=etPan.59e5aabd.37236a33.98a8@zdharma.org \
--to=psprint@zdharma.org \
--cc=zsh-workers@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).