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

Re: Expand array into multiple elements per item?



On Tue, 13 Sep 2011, Peter Stephenson wrote:

On Tue, 13 Sep 2011 04:15:50 -0400 "Benjamin R. Haskell" wrote:

Three questions:

1. How can I easily take:
     somelist=( 'a b' c 'd e' )

and get back:

     anotherlist=( -id 'a b' -id c -id 'd e' )

anotherlist=({-id,${^somelist}})

Elegant. I didn't think about brace expansion. Thanks, this is what I'll use.


2. ...relatedly, I'm confused by the following:

     $ somelist=( 'a b' c 'd e' )
(i)  $ print -l - $^somelist(e:'reply=( -id $REPLY )':)
     zsh: no matches found: a b(e:reply=( -id $REPLY ):)
(ii) $ print -l - $^somelist(Ne:'reply=( -id $REPLY )':)
     (...nothing printed...)
     $

Why does neither (i) nor (ii) work?

The main problem is that globbing flags rely on globbing; if there's no matching file, it doesn't work.

I think it's just the fact that the glob is expanded (and thus tested for the resultant filenames existing) before the qualifiers that trips me up, but yes: Don't use globs on non-files. Makes sense.


3. I thought I recalled a relatively recent addition to parameter expansion flags for just this use-case. But I can't seem to find the flag in zsh-4.3.12 patchlevel 1.5346. Still interested in the answer to the rest, regardless.

You might be thinking of the globbing flag, P.  If you did have files,
*(P:-id:) would have done what you wanted.  But you don't.  The actual
effect is a bit bizarre (turning off nomatch):

-id
a b(P:-id:)
-id
c(P:-id:)
-id
d e(P:-id:)

Interesting.  Yes.  'P' was the flag I failed to find.

--
Thanks,
Ben



Messages sorted by: Reverse Date, Date, Thread, Author