* 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
* Re: Array parameter expansion: take n-th word from array elements
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
0 siblings, 1 reply; 5+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2017-06-25 17:34 UTC (permalink / raw)
To: Alexander Groß, zsh-users
25.06.2017, 15:46, "Alexander Groß" <agross@therightstuff.de>:
> 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!
I would just go with ${#} and ${%}:
% foo=('a interesting - b' 'a also-interesting - c')
% echo ${${foo% - *}#* }
interesting also-interesting
>
> Alex
> --
> Alexander Groß
> http://therightstuff.de/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Array parameter expansion: take n-th word from array elements
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ß
0 siblings, 2 replies; 5+ messages in thread
From: Bart Schaefer @ 2017-06-25 23:15 UTC (permalink / raw)
To: zsh-users
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@therightstuff.de>:
} > 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]}}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Array parameter expansion: take n-th word from array elements
2017-06-25 23:15 ` Bart Schaefer
@ 2017-06-26 8:49 ` Nikolay Aleksandrovich Pavlov (ZyX)
2017-06-26 9:50 ` Alexander Groß
1 sibling, 0 replies; 5+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2017-06-26 8:49 UTC (permalink / raw)
To: Bart Schaefer, zsh-users
26.06.2017, 02:17, "Bart Schaefer" <schaefer@brasslantern.com>:
> Hmm, I never received Nikolay's message, though I see it on the zsh
> archive site.
I am periodically receiving messages that state that some of the messages sent by list to me bounced. Maybe this problem has the same origin.
>
> On Jun 25, 8:34pm, Nikolay Aleksandrovich Pavlov (ZyX) wrote:
> }
> } 25.06.2017, 15:46, "Alexander Gross" <agross@therightstuff.de>:
> } > 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]}}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Array parameter expansion: take n-th word from array elements
2017-06-25 23:15 ` Bart Schaefer
2017-06-26 8:49 ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2017-06-26 9:50 ` Alexander Groß
1 sibling, 0 replies; 5+ messages in thread
From: Alexander Groß @ 2017-06-26 9:50 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: text/plain, Size: 1009 bytes --]
>
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]}}
Thank you, that was a pointer in the right direction!
I have another use case for a different output, generated from grep this
time. Some strings may not contain the second word and I want to remove
them. Your suggestion prints the last word in this case. Can this be
filtered out as well?
$ foo=('source interesting' ' source also-interesting' 'source')
$ setopt extendedglob
$
print -lr -- ${foo/(#m)*/${MATCH[(w)2]}}
interesting
also-interesting
source
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).