Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
array element subsetting
- X-seq: zsh-users 14970
- From: "S. Cowles" <scowles@xxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: array element subsetting
- Date: Fri, 26 Mar 2010 02:24:49 -0700 (PDT)
- In-reply-to: <Xns9D45D21E0B19Azzappergmailcom@xxxxxxxxxxxx>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <Xns9D456D0F33B88zzappergmailcom@xxxxxxxxxxxx> <20100324120359.GA29984@xxxxxxxxxxxxxxxxxxxxxxxxxx> <Xns9D45D21E0B19Azzappergmailcom@xxxxxxxxxxxx>
I am trying to figure out the correct syntax for constructing two
one-liner subsetting operations on arrays. I have two objectives: 1)
select nth character from each array element, and 2) select nth element
within each array element.
The array these methods operate upon is something simple such as:
a=(
"satu two trio"
"sah funf seis"
"boundarycase"
"revert to pattern"
)
For the first case, the solution I came up with is:
print -l ${a//#%(#b)(?)*/${match[1]}}
for the first character of each element, or
print -l ${a//#%(#b)?(#c2)(?(#c1))*/${match[1]}}
for 3rd character of each element (generalizable to [n,m] elements).
For the second case, doing word splitting on each array element, I came up
with two variations to print out the second word in each element.
print -l ${a//#%(#b)*[[:IFS:]]##(*)[[:IFS:]]##*/${match[1]}}
print -l ${a//#%(#b)[[:WORD:]]##[^[:WORD:]]##([[:WORD:]]##)[^[:WORD:]]##*/${match[1]}}
(Though not important for my uses, these both fail with the boundary case
where the array element contains only one word.)
Isn't there a better/cleaner way to accomplish this, especially for the
second objective?
Thanks.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author