zsh-users
 help / color / mirror / code / Atom feed
From: "S. Cowles" <scowles@ckhb.org>
To: zsh-users@zsh.org
Subject: array element subsetting
Date: Fri, 26 Mar 2010 02:24:49 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LN8.2.00.1003260202111.10812@ckhb06.ckhb.org> (raw)
In-Reply-To: <Xns9D45D21E0B19Azzappergmailcom@80.91.229.10>


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.


  reply	other threads:[~2010-03-26  9:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-24 10:43 How much of it is zsh? zzapper
2010-03-24 11:04 ` Piotr Kalinowski
2010-03-24 12:03 ` Nadav Har'El
2010-03-24 19:49   ` Stephane Chazelas
2010-03-24 20:39   ` zzapper
2010-03-26  9:24     ` S. Cowles [this message]
2010-03-26 14:41       ` array element subsetting Bart Schaefer
2010-03-26 19:32         ` S. Cowles
2010-03-27  4:14           ` Bart Schaefer
2010-03-24 13:43 ` How much of it is zsh? Joke de Buhr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LN8.2.00.1003260202111.10812@ckhb06.ckhb.org \
    --to=scowles@ckhb.org \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).