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

Re: Array as parameter



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@xxxxxx> 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



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