From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13533 invoked by alias); 30 Sep 2016 19:07:27 -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: 21996 Received: (qmail 14078 invoked from network); 30 Sep 2016 19:07:27 -0000 X-Qmail-Scanner-Diagnostics: from mail-qk0-f174.google.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(209.85.220.174):SA:0(1.1/5.0):. Processed in 0.30796 secs); 30 Sep 2016 19:07:27 -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=1.1 required=5.0 tests=DATE_IN_PAST_06_12, FREEMAIL_FROM,SPF_PASS,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.1 X-Envelope-From: mikachu@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.220.174 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=IWn6anGrqfLV4ZUu+6uk2+hyq69bcr/16rLorLuLvBo=; b=yKCq9S4W/4QQze6t+c3yr6dAX0hNStJxoqNezYZnNNXaq/7OARfdWMg+xfCwrG21FA +p0px6HCNqwsSciHGNM4PEM0nSVKdClVq19LwAJznzv/oZw3usoLRXcp/ffm6vXVdW0K /ZsNa6c5QYkccyCT0f11UL8JTepnjgiNZa5FvRyeRtm+L61TeR8iMOewX+dYGSG1Fj2z Ms8BrMoLUK+4xMQEr4qMNNUzBPSgPYng9fxAejHyEsLXTMmXqQMzAjt4uyq1xwVWL3q6 ynR/e1wtSwLCDguq2MKE2/tKAB4i37ppa+BmGkZe9b+qNEjTCWJrXTEHnKxpFr0+8f58 JbIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=IWn6anGrqfLV4ZUu+6uk2+hyq69bcr/16rLorLuLvBo=; b=ESkJqm95Ml+QeTOIbDWDGM5G0zo4Kw0RfL84r99OoZ5LEIzDwWx9yrq6TrTnvC+qLs YDiiUJgWC/lnGe0eRQEBtWDbDzx46F8L2gRswh4ubP3H74OsNLHHgJNvi5qaTGgrzFFn UG4C37hcNVIZim0xbBhzVWBw4HSpkSyDzJ53YnE9cbQdJbybdNDSR3BAmj+uZDlaDm8y UFLgtVCVwMI8cmfrbcTc0UOEoq+ICb74XOaVHf00rY76kLb/p3Mxf05HY4H0KU+Zt6KS dZKQfoHcLr8Oa5BXlaBCkFt2GX2SEJqA+OYrqmoShV9mbE1gJFNBfQyeJ8eY4DEv2Jt9 n7GA== X-Gm-Message-State: AA6/9RkbVhN9+tN8pEeBzZCUzcoEdVu7H9fb8tF7SE669qBYhbm3gpHyUEEA8cw2U7zANP+6LPlFQpJByUyKqQ== X-Received: by 10.55.94.70 with SMTP id s67mr5530464qkb.117.1475224099645; Fri, 30 Sep 2016 01:28:19 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20160930074509.GA2554@fujitsu.shahaf.local2> References: <1A643BF6-28C9-483E-9312-D8C856D41F0D__26800.772539764$1475219850$gmane$org@gmail.com> <20160930074509.GA2554@fujitsu.shahaf.local2> From: Mikael Magnusson Date: Fri, 30 Sep 2016 10:28:19 +0200 Message-ID: Subject: Re: Passing array to function To: Daniel Shahaf Cc: Ignatius Reilly , Zsh Users Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Fri, Sep 30, 2016 at 9:45 AM, Daniel Shahaf wro= te: > Ignatius Reilly wrote on Fri, Sep 30, 2016 at 02:15:55 -0500: >> addtoarray() { [[ -d $1 ]] && myarray=3D($1 $myarray) } >> >> which works as expected, prepending an element to an array only if its a= n >> existing directory. I'd like to rewrite this function so that I can pas= s >> 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]=3D${(q)1}" > } > > Explanation: > > - The (q) are there to convert the values to command-line-quoted > strings, for eval. $2 probably needs no quoting =E2=80=94 if it did, t= he eval > would see a syntax error =E2=80=94 but I put the (q) anyway to guard ag= ainst > invalid inputs (bobby tables attacks against the eval). > > - After parameter substitution, the resultant string is: > myarray[1,0]=3D/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. addtoarray() { [[ -d $1 ]] || return; local p=3D"$2[1,0]"; : ${(P)p::=3D$1}= } I forgot about [1,0] yesterday when you asked on IRC. --=20 Mikael Magnusson