zsh-workers
 help / color / mirror / code / Atom feed
* Documentation for code execution in substitutions using patterns
@ 2017-10-17  7:01 Sebastian Gniazdowski
  0 siblings, 0 replies; only message in thread
From: Sebastian Gniazdowski @ 2017-10-17  7:01 UTC (permalink / raw)
  To: zsh-workers

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-10-17  7:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-17  7:01 Documentation for code execution in substitutions using patterns Sebastian Gniazdowski

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).