Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: array element subsetting
- X-seq: zsh-users 14972
- From: "S. Cowles" <scowles@xxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: array element subsetting
- Date: Fri, 26 Mar 2010 12:32:02 -0700 (PDT)
- In-reply-to: <100326074148.ZM29047@xxxxxxxxxxxxxxxxxxxxxx>
- 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> <alpine.LN8.2.00.1003260202111.10812@xxxxxxxxxxxxxxx> <100326074148.ZM29047@xxxxxxxxxxxxxxxxxxxxxx>
On Fri, 26 Mar 2010, Bart Schaefer wrote:
Date: Fri, 26 Mar 2010 07:41:48 -0700
From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
On Mar 26, 2:24am, S. Cowles wrote:
} 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=(
} ...
} )
(1) can be done with the (M) parameter flag and simple head/tail:
print ${(M)a#?}
Simpler and more straightforward than backreferencing. Thank you, Bart.
(2) is more difficult to do without looping, because zsh doesn't
support multidimensional arrays, so you have to force an eval step
via the (e) flag:
print ${(e):-'${${=:-'${^a}' ""}[2]}'}
I hadn't previously used the parameter expansion (e) or array creation
${=...} methods. The inline array element addition is new to me; I missed
it in Peter's Manual, book, and the zshall man page.
Would it be worth considering adding a new subsection on Array Subsetting
to the ARRAY PARAMETERS section of the man pages, just after the Subscript
Parsing section and just prior to POSITIONAL PARAMETERS?
Messages sorted by:
Reverse Date,
Date,
Thread,
Author