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

Documentation for code execution in substitutions using patterns



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



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