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

Re: Array as parameter



On 2 November 2011 13:13, Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> On 2 November 2011 13:06, Stephane Chazelas <stephane_chazelas@xxxxxxxx> wrote:
>> 2011-11-02 11:14:38 +0100, Mikael Magnusson:
>>> On 2 November 2011 10:10, Stephane CHAZELAS <stephane_chazelas@xxxxxxxx> 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



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