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

Re: Array parameter expansion: take n-th word from array elements



Hmm, I never received Nikolay's message, though I see it on the zsh
archive site.

On Jun 25,  8:34pm, Nikolay Aleksandrovich Pavlov (ZyX) wrote:
}
} 25.06.2017, 15:46, "Alexander Gross" <agross@xxxxxxxxxxxxxxxx>:
} > Hi all,
} >
} > I would like to parse some output from a command that generates an array of
} > the following elements:
} >
} > Main System.Xml.XPath - 4.3
} > Main System.Xml.XPath.XDocument - 4.3
} > Test Castle.Core - 4.0
} > Test FsCheck - 2.9
} >
} > print -l ${${(s. .)foo}[2]}

The only parameter substitution operator that iterates over elements of
an array in the way you want is the ${NAME/PATTERN/REPL} syntax.  If
you setopt extendedglob and use (#m) in the pattern, $MATCH will give
you each of the elements as a string, so you next need to interpret
each string as words.  The (w) subscript modifier does that.

Thus:

    setopt extendedglob
    print -lr -- ${foo/(#m)*/${MATCH[(w)2]}}



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