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

Re: array element subsetting



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