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

Re: Array as parameter



2011-11-2, 04:35(+01), meino.cramer@xxxxxx:
> 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




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