From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25774 invoked by alias); 30 Sep 2016 07:46:33 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 21994 Received: (qmail 4679 invoked from network); 30 Sep 2016 07:46:33 -0000 X-Qmail-Scanner-Diagnostics: from out1-smtp.messagingengine.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(66.111.4.25):SA:0(0.0/5.0):. Processed in 0.121593 secs); 30 Sep 2016 07:46:33 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: d.s@daniel.shahaf.name X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: none (ns1.primenet.com.au: domain at daniel.shahaf.name does not designate permitted sender hosts) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= daniel.shahaf.name; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=mesmtp; bh=Y1TMzPBxhJz6W6Of ACngiI/BGhc=; b=saRXnCMnCPYlYOTVqn1/VHM4JghSyWykKWusJsOZgfb0pvGO TRgIWAGurjJ2TXlGbDJl74maGeKLT/LqqGHy50Q+BsavGFL3QhkUi03EKHEJHNOb m0dSO/PhvTSOiUen51W4tNUqpr/BlgVWIoIIQQYMJ2xo1eA5OQ3Iy0ta0l8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=Y1TMzPBxhJz6W6O fACngiI/BGhc=; b=fx8uQiP7bGeh8TaXUTUEMwYa2qYuDvJtUS6v9yIgVj3JfHq qlPUEcUq2K7R8LzWilgCkpMOZsh0fmH3z+6ATUwCabAJOZp6yTk6X+Ye3mulAa+Q MperDCRy8v9quAm9Go2DY1YLVSVgocaraixRvnSOgCHAV8DZKn7DqB18Yp9M= X-Sasl-enc: oCHSiI8JTBOwl7KqP9on9T9cz6pqHhp5k0s4v1vYZdT3 1475221590 Date: Fri, 30 Sep 2016 07:45:09 +0000 From: Daniel Shahaf To: Ignatius Reilly Cc: zsh-users@zsh.org Subject: Re: Passing array to function Message-ID: <20160930074509.GA2554@fujitsu.shahaf.local2> References: <1A643BF6-28C9-483E-9312-D8C856D41F0D__26800.772539764$1475219850$gmane$org@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1A643BF6-28C9-483E-9312-D8C856D41F0D__26800.772539764$1475219850$gmane$org@gmail.com> User-Agent: Mutt/1.5.23 (2014-03-12) Ignatius Reilly wrote on Fri, Sep 30, 2016 at 02:15:55 -0500: > addtoarray() { [[ -d $1 ]] && myarray=($1 $myarray) } > > which works as expected, prepending an element to an array only if its an > existing directory. I'd like to rewrite this function so that I can pass > the array name as a parameter like so: > > addtoarray /usr/foo myarray You could do it with eval: addtoarray() { [[ -d $1 ]] && eval "${(q)2}[1,0]=${(q)1}" } Explanation: - The (q) are there to convert the values to command-line-quoted strings, for eval. $2 probably needs no quoting — if it did, the eval would see a syntax error — but I put the (q) anyway to guard against invalid inputs (bobby tables attacks against the eval). - After parameter substitution, the resultant string is: myarray[1,0]=/usr/foo which is a slice assignment that prepends an element to the named array. If there's a solution without eval I'm sure someone will post it. Cheers, Daniel > Thanks >