* Re: Array as parameter [not found] <20111102033545.GI28043__714.249446946447$1320205564$gmane$org@solfire> @ 2011-11-02 9:04 ` Stephane CHAZELAS 0 siblings, 0 replies; 6+ messages in thread From: Stephane CHAZELAS @ 2011-11-02 9:04 UTC (permalink / raw) To: zsh-workers 2011-11-2, 04:35(+01), meino.cramer@gmx.de: > how can I use an array as a parameter to a function like > this (example does not work...just as an explanation, what > I want to do) > > #! /bin/zsh > funcion arrprint() > { > for i in $1 > do > echo $i > echo "---" > done > } > > a=( 1 2 3 4 5 6 ) > > arrprint a [...] Try (pass by values) arrprint() { for i do echo "$i" echo --- done } arrprint "${a[@]}" Or (pass by name) arrprint() { eval 'set -- "${'$1'[@]}"' for i do echo "$i" echo --- done } arrprint a -- Stephane ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <20111102033545.GI28043@solfire>]
[parent not found: <CAHSx_SsGPHWkGfspXKo39oZ6G3Rd+H-8_2PAuixHmW94d4VCEg__34702.1825182766$1320210589$gmane$org@mail.gmail.com>]
* Re: Array as parameter [not found] ` <CAHSx_SsGPHWkGfspXKo39oZ6G3Rd+H-8_2PAuixHmW94d4VCEg__34702.1825182766$1320210589$gmane$org@mail.gmail.com> @ 2011-11-02 9:10 ` Stephane CHAZELAS 2011-11-02 10:14 ` Mikael Magnusson 0 siblings, 1 reply; 6+ messages in thread From: Stephane CHAZELAS @ 2011-11-02 9:10 UTC (permalink / raw) To: zsh-workers 2011-11-1, 22:00(-07), Wayne Davison: > --000e0cd299861c09c804b0b95bb8 > Content-Type: text/plain; charset=UTF-8 > > On Tue, Nov 1, 2011 at 8:35 PM, <meino.cramer@gmx.de> wrote: > >> arrprint a >> > > That passes the string "a" to the function. One thing you can do is to > pass the array as separate parameters (arrprint $a) $a discards the empty elements in the "a" array. Use "${a[@]}" instead. > and then use "for i in > $*" in your function $* in zsh also discards the empty elements (it's even worse in compatibility mode with other shells where word splitting and filename generation may also be done). Use "$@" instead. Or better, write it "for i do". > However, if you need to be able to keep the array > parameter separate from other parameters, you could instead refer to the > variable whose name you passed in using ${(P)1} in your function in place > of the $1. Except that it doesn't work for arrays. You'd need to use eval here. -- Stephane ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Array as parameter 2011-11-02 9:10 ` Stephane CHAZELAS @ 2011-11-02 10:14 ` Mikael Magnusson 2011-11-02 12:06 ` Stephane Chazelas 0 siblings, 1 reply; 6+ messages in thread From: Mikael Magnusson @ 2011-11-02 10:14 UTC (permalink / raw) To: zsh-workers On 2 November 2011 10:10, Stephane CHAZELAS <stephane_chazelas@yahoo.fr> wrote: > 2011-11-1, 22:00(-07), Wayne Davison: >> However, if you need to be able to keep the array >> parameter separate from other parameters, you could instead refer to the >> variable whose name you passed in using ${(P)1} in your function in place >> of the $1. > > Except that it doesn't work for arrays. You'd need to use eval > here. (P) works perfectly fine with arrays. -- Mikael Magnusson ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Array as parameter 2011-11-02 10:14 ` Mikael Magnusson @ 2011-11-02 12:06 ` Stephane Chazelas 2011-11-02 12:13 ` Mikael Magnusson 0 siblings, 1 reply; 6+ messages in thread From: Stephane Chazelas @ 2011-11-02 12:06 UTC (permalink / raw) To: Mikael Magnusson; +Cc: zsh-workers 2011-11-02 11:14:38 +0100, Mikael Magnusson: > On 2 November 2011 10:10, Stephane CHAZELAS <stephane_chazelas@yahoo.fr> wrote: > > 2011-11-1, 22:00(-07), Wayne Davison: > >> However, if you need to be able to keep the array > >> parameter separate from other parameters, you could instead refer to the > >> variable whose name you passed in using ${(P)1} in your function in place > >> of the $1. > > > > Except that it doesn't work for arrays. You'd need to use eval > > here. > > (P) works perfectly fine with arrays. [...] Indeed, though it seems you have to write it as "${${(@P)1}[@]}", I'm not too sure why. ~/install/cvs/pixz$ zsh -c 'a=("" "a b" c d); b=a; print -l "${${(P)b}[@]}"' a b c d ~/install/cvs/pixz$ zsh -c 'a=("" "a b" c d); b=a; print -l "${${(@P)b}[@]}"' a b c d -- Stephane ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Array as parameter 2011-11-02 12:06 ` Stephane Chazelas @ 2011-11-02 12:13 ` Mikael Magnusson 2011-11-02 12:15 ` Mikael Magnusson 0 siblings, 1 reply; 6+ messages in thread From: Mikael Magnusson @ 2011-11-02 12:13 UTC (permalink / raw) To: Mikael Magnusson, zsh-workers On 2 November 2011 13:06, Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote: > 2011-11-02 11:14:38 +0100, Mikael Magnusson: >> On 2 November 2011 10:10, Stephane CHAZELAS <stephane_chazelas@yahoo.fr> wrote: >> > 2011-11-1, 22:00(-07), Wayne Davison: >> >> However, if you need to be able to keep the array >> >> parameter separate from other parameters, you could instead refer to the >> >> variable whose name you passed in using ${(P)1} in your function in place >> >> of the $1. >> > >> > Except that it doesn't work for arrays. You'd need to use eval >> > here. >> >> (P) works perfectly fine with arrays. > [...] > > Indeed, though it seems you have to write it as "${${(@P)1}[@]}", I'm not too sure why. You need to specify @ at every nesting level that's inside double quotes to prevent scalar joining. At the ${(P)b[@]} level, if that was an implied question, the subscript is applied first: > ~/install/cvs/pixz$ zsh -c 'a=("" "a b" c d); b=a; print -l "${${(P)b}[@]}"' > a b c d > ~/install/cvs/pixz$ zsh -c 'a=("" "a b" c d); b=a; print -l "${${(@P)b}[@]}"' > > a b > c > d But this is shorter, % zsh -c 'a=("" "a b" c d); b=a; print -l "${(@P)b}"' a b c d -- Mikael Magnusson ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Array as parameter 2011-11-02 12:13 ` Mikael Magnusson @ 2011-11-02 12:15 ` Mikael Magnusson 0 siblings, 0 replies; 6+ messages in thread From: Mikael Magnusson @ 2011-11-02 12:15 UTC (permalink / raw) To: zsh-workers On 2 November 2011 13:13, Mikael Magnusson <mikachu@gmail.com> wrote: > On 2 November 2011 13:06, Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote: >> 2011-11-02 11:14:38 +0100, Mikael Magnusson: >>> On 2 November 2011 10:10, Stephane CHAZELAS <stephane_chazelas@yahoo.fr> wrote: >>> > 2011-11-1, 22:00(-07), Wayne Davison: >>> >> However, if you need to be able to keep the array >>> >> parameter separate from other parameters, you could instead refer to the >>> >> variable whose name you passed in using ${(P)1} in your function in place >>> >> of the $1. >>> > >>> > Except that it doesn't work for arrays. You'd need to use eval >>> > here. >>> >>> (P) works perfectly fine with arrays. >> [...] >> >> Indeed, though it seems you have to write it as "${${(@P)1}[@]}", I'm not too sure why. > > You need to specify @ at every nesting level that's inside double > quotes to prevent scalar joining. At the ${(P)b[@]} level, if that was > an implied question, the subscript is applied first: [apparently some keycombo i just pressed sends the message in gmail, i wonder what it was] % zsh -c 'a=("" "a b" c d); b=(b a); print -l "${(@P)b[2]}"' a b c d % zsh -c 'a=("" "a b" c d); b=(b a); print -l "${(@P)b[1]}"' b a >> ~/install/cvs/pixz$ zsh -c 'a=("" "a b" c d); b=a; print -l "${${(P)b}[@]}"' >> a b c d >> ~/install/cvs/pixz$ zsh -c 'a=("" "a b" c d); b=a; print -l "${${(@P)b}[@]}"' >> >> a b >> c >> d > > But this is shorter, > % zsh -c 'a=("" "a b" c d); b=a; print -l "${(@P)b}"' > > a b > c > d -- Mikael Magnusson ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2011-11-02 12:18 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- [not found] <20111102033545.GI28043__714.249446946447$1320205564$gmane$org@solfire> 2011-11-02 9:04 ` Array as parameter Stephane CHAZELAS [not found] <20111102033545.GI28043@solfire> [not found] ` <CAHSx_SsGPHWkGfspXKo39oZ6G3Rd+H-8_2PAuixHmW94d4VCEg__34702.1825182766$1320210589$gmane$org@mail.gmail.com> 2011-11-02 9:10 ` Stephane CHAZELAS 2011-11-02 10:14 ` Mikael Magnusson 2011-11-02 12:06 ` Stephane Chazelas 2011-11-02 12:13 ` Mikael Magnusson 2011-11-02 12:15 ` Mikael Magnusson
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).