Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Passing array to function
- X-seq: zsh-users 21995
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Passing array to function
- Date: Fri, 30 Sep 2016 11:26:15 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version; bh=KwwccUSpnD1H/lXPpgg1wrENgOwcMY9j6MjGOMnO+Wc=; b=mBFUuuVxwr1BJ72OkJ5pKJiMwTu4/H8/iAUUZIxI8Xz+BP6a1EMZwPXQTwXEnNWIR4 gpjR8WFS2PB1QQIvLYFUpTHG+NO4km3eHePWtGY+httSsIKKP0PhJe3IWLlYx4gXdUIW xsMaqZ9Bx4SbCA9z0CSEa1FfVvh4FslO2pSzqCaCFGTJnAC17f+yGV87vlAMRa26BlL7 /sajMgSQ707Qy2z9eDbBMEo9gangXS2+GsbyFGNLYWUW5E0a/zc5A3pD4IcJ31f8Cv0V G0s9Vy6mlJQu+IUZMET0xvdxuyv0gG+lDlcXbprB/JL378KdIWFqI9hv61j+w+jJnJEu vMRQ==
- In-reply-to: <20160930074509.GA2554@fujitsu.shahaf.local2>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <1A643BF6-28C9-483E-9312-D8C856D41F0D__26800.772539764$1475219850$gmane$org@gmail.com> <20160930074509.GA2554@fujitsu.shahaf.local2>
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