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

Re: Passing array to function



On Sep 30,  7:45am, Daniel Shahaf wrote:
} Subject: Re: Passing array to function
}
} Ignatius Reilly wrote on Fri, Sep 30, 2016 at 02:15:55 -0500:
} > addtoarray() { [[ -d $1 ]] && myarray=($1 $myarray) }
} > 
} > addtoarray /usr/foo myarray
} 
} addtoarray() {
}   [[ -d $1 ]] && eval "${(q)2}[1,0]=${(q)1}"
} }

Solution without eval:

    addtoarray() { [[ -d $1 ]] && set -A $2 $1 ${(P)2}  }

Various quoting may be necessary if you using an emulation mode or
other nonstandard setopts, but from your original example it does
not appear so.  ${(P)2} means to treat the value of $2 as a name
and expand that named parameter.



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