Hello, I thought that I share a method of applying of function to each element of an array. I think that it's obvious for someone interested in // substitution and (#m) and (#b) flags, but not necessarily for others. Also, it would be much more beautiful if zsh would support mksh's substitution ${|func;} which is replaced by the value that func assigns to REPLY, assuming that func can take some arguments (i.e. the $MATCH in the use case): msfunc() { REPLY="${(Q)1} $RANDOM"; return 0; } functions -Ms msfunc 1 1 msfunc myarr=( value1 value2 abc1 abc2 ) myarr=( "${(@)${myarr[@]/(#m)*/$(( msfunc(${(q)MATCH}) ))$REPLY}/(#s)0/}" ) print -rl $myarr Output: value1 21720 value2 18920 abc1 3318 abc2 14483 The (#s) matches the start of the string and is just for to ensure correctness and not actually needed. It however might save some time when debugging code, when some 0s start to disappear from the strings because of an specific, not always matching pattern (with (#b) flag, for example). The 0 can be also changed to some random number in such case. -- Sebastian Gniazdowski News: https://twitter.com/ZdharmaI IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin blog: http://zdharma.org
On Thu, 29 Aug 2019 at 02:49, Sebastian Gniazdowski <sgniazdowski@gmail.com> wrote: > ... > assigns to REPLY, assuming that func can take some arguments (i.e. the > $MATCH in the use case): This is not needed – the function could use $MATCH directly. I was assuming that adding such substitution would require parser alteration, however it doesn't seem to: % echo ${|foo;} zsh: bad substitution So the syntax correctly passes the parser and meets paramsubst() or stringsubst(). So adding such substitution should be easy – just a doshfunc(() followed by getstrvalue("REPLY"). If there are any pitfalls in it then let me know it. -- Sebastian Gniazdowski News: https://twitter.com/ZdharmaI IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin Blog: http://zdharma.org
On Thu, Aug 29, 2019 at 2:51 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> myarr=( "${(@)${myarr[@]/(#m)*/$(( msfunc(${(q)MATCH}) ))$REPLY}/(#s)0/}" )
I usually use ${foo+} to expand foo for its side effects and to ignore
the substituted value.
myarr=( "${(@)${myarr[@]/(#m)*/${$((msfunc(${(q)MATCH})))+}$REPLY}}" )
This way you can discard the substituted value without knowing what it is.
Roman.
[-- Attachment #1: Type: text/plain, Size: 621 bytes --] Cool, thanks! It makes the solution much more readable. czw., 29 sie 2019, 12:47 użytkownik Roman Perepelitsa < roman.perepelitsa@gmail.com> napisał: > On Thu, Aug 29, 2019 at 2:51 AM Sebastian Gniazdowski > <sgniazdowski@gmail.com> wrote: > > > > myarr=( "${(@)${myarr[@]/(#m)*/$(( msfunc(${(q)MATCH}) > ))$REPLY}/(#s)0/}" ) > > I usually use ${foo+} to expand foo for its side effects and to ignore > the substituted value. > > myarr=( "${(@)${myarr[@]/(#m)*/${$((msfunc(${(q)MATCH})))+}$REPLY}}" ) > > This way you can discard the substituted value without knowing what it is. > > Roman. >