zsh-users
 help / color / mirror / code / Atom feed
* Array parameter expansion: take n-th word from array elements
@ 2017-06-25 12:44 Alexander Groß
  2017-06-25 17:34 ` Nikolay Aleksandrovich Pavlov (ZyX)
  0 siblings, 1 reply; 5+ messages in thread
From: Alexander Groß @ 2017-06-25 12:44 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 960 bytes --]

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

E.g.

foo=('a interesting - b' 'a also-interesting - c')

I would like to take the second word from each and every of the array
elements (interesting also-interesting). Unfortunately,

print -l ${foo[2]}

prints the second element, 'a also-interesting - c'.

A for loop works:

local -a filtered
for package in $foo; do
  # Split by space and take second word.
  filtered+="${${(s. .)package}[2]}"
done

But I wonder if there is a better way using parameter expansion. Here's
what I came up with so far:
print -l ${${(s. .)foo}[2]}

Unfortunately, it just prints 'interesting'.

Any ideas pointing me in the right direction are greatly appreciated!

Alex
-- 
Alexander Groß
http://therightstuff.de/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-06-26  9:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-25 12:44 Array parameter expansion: take n-th word from array elements Alexander Groß
2017-06-25 17:34 ` Nikolay Aleksandrovich Pavlov (ZyX)
2017-06-25 23:15   ` Bart Schaefer
2017-06-26  8:49     ` Nikolay Aleksandrovich Pavlov (ZyX)
2017-06-26  9:50     ` Alexander Groß

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