* Expand array into multiple elements per item? @ 2011-09-13 8:15 Benjamin R. Haskell 2011-09-13 8:36 ` Anthony Charles 2011-09-13 8:57 ` Peter Stephenson 0 siblings, 2 replies; 8+ messages in thread From: Benjamin R. Haskell @ 2011-09-13 8:15 UTC (permalink / raw) To: Zsh Users 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' ) 2. ...relatedly, I'm confused by the following: $ somelist=( 'a b' c 'd e' ) $ print -l - $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? 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. -- Best, Ben ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Expand array into multiple elements per item? 2011-09-13 8:15 Expand array into multiple elements per item? Benjamin R. Haskell @ 2011-09-13 8:36 ` Anthony Charles 2011-09-13 8:52 ` Benjamin R. Haskell 2011-09-13 8:57 ` Peter Stephenson 1 sibling, 1 reply; 8+ messages in thread From: Anthony Charles @ 2011-09-13 8:36 UTC (permalink / raw) To: Benjamin R. Haskell; +Cc: Zsh Users Hi, You can try this : print -l -- "-id "$^somelist anotherlist=( "-id "$^somelist ) But I can't give you any answer for question 2 :/ -- Anthony CHARLES On Tue, Sep 13, 2011 at 04:15:50AM -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' ) > > 2. ...relatedly, I'm confused by the following: > > $ somelist=( 'a b' c 'd e' ) > $ print -l - $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? > > 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. > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Expand array into multiple elements per item? 2011-09-13 8:36 ` Anthony Charles @ 2011-09-13 8:52 ` Benjamin R. Haskell 2011-09-15 18:07 ` Oliver Kiddle 0 siblings, 1 reply; 8+ messages in thread From: Benjamin R. Haskell @ 2011-09-13 8:52 UTC (permalink / raw) To: Anthony Charles; +Cc: Zsh Users On Tue, 13 Sep 2011, Anthony Charles wrote: > Hi, > > You can try this : > print -l -- "-id "$^somelist > > anotherlist=( "-id "$^somelist ) > > But I can't give you any answer for question 2 :/ anotherlist should end up as I specified before: $ anotherlist=( -id 'a b' -id c -id 'd e' ) I.e. separate elements, but not just word-splitting everything: $ print -l - $^anotherlist -id <-- separate a b <-- kept as one argument -id <-- separate c -id <-- separate d e <-- kept as one argument With your suggestions the -id isn't a separate element: $ print -l - "-id "$^somelist -id a b -id c -id d e $ anotherlist=( "-id "$^somelist ) $ print -l - $^anotherlist -id a b -id c -id d e -- Best, Ben ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Expand array into multiple elements per item? 2011-09-13 8:52 ` Benjamin R. Haskell @ 2011-09-15 18:07 ` Oliver Kiddle 0 siblings, 0 replies; 8+ messages in thread From: Oliver Kiddle @ 2011-09-15 18:07 UTC (permalink / raw) To: Zsh Users On 13 Sep, "Benjamin R. Haskell" wrote: > > You can try this : > > print -l -- "-id "$^somelist > > anotherlist should end up as I specified before: > > $ anotherlist=( -id 'a b' -id c -id 'd e' ) > > I.e. separate elements, but not just word-splitting everything: If you join the list up and then re-split it, it might work: print -l -- ${(0)${(pj:\0:):--id$'\0'$a}} Oliver ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Expand array into multiple elements per item? 2011-09-13 8:15 Expand array into multiple elements per item? Benjamin R. Haskell 2011-09-13 8:36 ` Anthony Charles @ 2011-09-13 8:57 ` Peter Stephenson 2011-09-13 10:19 ` Mikael Magnusson 2011-09-13 17:21 ` Benjamin R. Haskell 1 sibling, 2 replies; 8+ messages in thread From: Peter Stephenson @ 2011-09-13 8:57 UTC (permalink / raw) To: Zsh Users On Tue, 13 Sep 2011 04:15:50 -0400 "Benjamin R. Haskell" <zsh@benizi.com> 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}}) > 2. ...relatedly, I'm confused by the following: > > $ somelist=( 'a b' c 'd e' ) > $ print -l - $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. You'd probably want "oN" in the globbing flags to turn off sorting if you did have files. > 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:) -- Peter Stephenson <pws@csr.com> Software Engineer Tel: +44 (0)1223 692070 Cambridge Silicon Radio Limited Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Expand array into multiple elements per item? 2011-09-13 8:57 ` Peter Stephenson @ 2011-09-13 10:19 ` Mikael Magnusson 2011-09-13 17:32 ` Benjamin R. Haskell 2011-09-13 17:21 ` Benjamin R. Haskell 1 sibling, 1 reply; 8+ messages in thread From: Mikael Magnusson @ 2011-09-13 10:19 UTC (permalink / raw) To: Zsh Users On 13 September 2011 10:57, Peter Stephenson <Peter.Stephenson@csr.com> wrote: >> 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. You'd probably want "oN" in the > globbing flags to turn off sorting if you did have files. > >> 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:) One fun thing you can do is use .(e,'reply=(foo bar baz)',) if you want to use some other globbing flags on your data. It's not applicable for this problem, at least I can't think of a way. I did suggest it on irc once for sorting an array by the basename: 22:39 <offbyone> I want to sort this by basename so that I order the elements in this pair of expansions asciibetically regardless of the directory they're found in. 22:39 <offbyone> (so, for example, if I had A/02_something B/01_something B/03_something, it'd come out as B/01_something A/02_something B/03_something) 22:47 <Mikachu> a=( oneglob anotherglob ) 22:47 <Mikachu> echo .(e:'reply=($a)':oe,'REPLY=$REPLY:t',) It would be nice to have a similar mechanism for sorting arrays maybe :). Oh wait, I just realized it does work, just do this: % print -l - .(e:'reply=($somelist)':P:-id:) -id a b -id c -id d e Obviously, this does qualify in the 'a bit of a hack' category. (And yes, it does fail if $PWD is chmoded -x, maybe / is a safer bet.) -- Mikael Magnusson ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Expand array into multiple elements per item? 2011-09-13 10:19 ` Mikael Magnusson @ 2011-09-13 17:32 ` Benjamin R. Haskell 0 siblings, 0 replies; 8+ messages in thread From: Benjamin R. Haskell @ 2011-09-13 17:32 UTC (permalink / raw) To: Mikael Magnusson; +Cc: Zsh Users On Tue, 13 Sep 2011, Mikael Magnusson wrote: > On 13 September 2011 10:57, Peter Stephenson wrote: >>> 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. [...] >> > > One fun thing you can do is use .(e,'reply=(foo bar baz)',) if you > want to use some other globbing flags on your data. It's not > applicable for this problem, at least I can't think of a way. I did > suggest it on irc once for sorting an array by the basename: > > 22:39 <offbyone> I want to sort this by basename so that I order the > elements in this pair of expansions asciibetically regardless of the > directory they're found in. > 22:39 <offbyone> (so, for example, if I had A/02_something > B/01_something B/03_something, it'd come out as B/01_something > A/02_something B/03_something) > 22:47 <Mikachu> a=( oneglob anotherglob ) > 22:47 <Mikachu> echo .(e:'reply=($a)':oe,'REPLY=$REPLY:t',) > > It would be nice to have a similar mechanism for sorting arrays maybe :). +1. > Oh wait, I just realized it does work, just do this: > > % print -l - .(e:'reply=($somelist)':P:-id:) > -id > a b > -id > c > -id > d e > > Obviously, this does qualify in the 'a bit of a hack' category. (And > yes, it does fail if $PWD is chmoded -x, maybe / is a safer bet.) Cool. Nice hack. -- Best, Ben ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Expand array into multiple elements per item? 2011-09-13 8:57 ` Peter Stephenson 2011-09-13 10:19 ` Mikael Magnusson @ 2011-09-13 17:21 ` Benjamin R. Haskell 1 sibling, 0 replies; 8+ messages in thread From: Benjamin R. Haskell @ 2011-09-13 17:21 UTC (permalink / raw) To: Peter Stephenson; +Cc: Zsh Users 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2011-09-15 18:13 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2011-09-13 8:15 Expand array into multiple elements per item? Benjamin R. Haskell 2011-09-13 8:36 ` Anthony Charles 2011-09-13 8:52 ` Benjamin R. Haskell 2011-09-15 18:07 ` Oliver Kiddle 2011-09-13 8:57 ` Peter Stephenson 2011-09-13 10:19 ` Mikael Magnusson 2011-09-13 17:32 ` Benjamin R. Haskell 2011-09-13 17:21 ` Benjamin R. Haskell
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).