zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: "S. Cowles" <scowles@ckhb.org>, zsh-users@zsh.org
Subject: Re: array element subsetting
Date: Fri, 26 Mar 2010 07:41:48 -0700	[thread overview]
Message-ID: <100326074148.ZM29047@torch.brasslantern.com> (raw)
In-Reply-To: <alpine.LN8.2.00.1003260202111.10812@ckhb06.ckhb.org>

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=(
}      "satu two trio"
}      "sah funf seis"
}      "boundarycase"
}      "revert to pattern"
} )

(1) can be done with the (M) parameter flag and simple head/tail:

	print ${(M)a#?}

To generalize to the Nth element, ${(M)${(M)a#?(#c$N)}%?} (requires
extendedglob, of course).

(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]}'}

However, this yields the second character of arrays that contain
only one word, because ${=...} reduces singular arrays to scalars.
A simple workaround is to insert an empty dummy element at the tail:

	print ${(e):-'${${=:-'${^a}' ""}[2]}'}

In the event there are special characters in the strings in $a, an
extra level of quoting can be added and then removed:

	print ${(e):-'${${=${(Q):-'${(q)^a}' ""}}[2]}'}

However, this removes again the empty element inserted by the double
quotes, i.e., it returns nothing for the short array rather than an
empty second element (use "print -l" in those examples to see the
difference more clearly).  Remove the (e) if you want to see what's
going on with the ${(q)^a} business.


  reply	other threads:[~2010-03-26 14:42 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     ` array element subsetting S. Cowles
2010-03-26 14:41       ` Bart Schaefer [this message]
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=100326074148.ZM29047@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=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).