zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: Ignatius Reilly <turnbuckle@gmail.com>, Zsh Users <zsh-users@zsh.org>
Subject: Re: Passing array to function
Date: Fri, 30 Sep 2016 10:28:19 +0200	[thread overview]
Message-ID: <CAHYJk3RCgUk+sz=kk-g3qdgHXV9T1FxCQVdiHZcd=AM_U_fwPA@mail.gmail.com> (raw)
In-Reply-To: <20160930074509.GA2554@fujitsu.shahaf.local2>

On Fri, Sep 30, 2016 at 9:45 AM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> 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.

addtoarray() { [[ -d $1 ]] || return; local p="$2[1,0]"; : ${(P)p::=$1} }
I forgot about [1,0] yesterday when you asked on IRC.

-- 
Mikael Magnusson


  reply	other threads:[~2016-09-30 19:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1A643BF6-28C9-483E-9312-D8C856D41F0D__26800.772539764$1475219850$gmane$org@gmail.com>
2016-09-30  7:45 ` Daniel Shahaf
2016-09-30  8:28   ` Mikael Magnusson [this message]
2016-09-30 18:26   ` Bart Schaefer
2016-09-30  7:15 Ignatius Reilly

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHYJk3RCgUk+sz=kk-g3qdgHXV9T1FxCQVdiHZcd=AM_U_fwPA@mail.gmail.com' \
    --to=mikachu@gmail.com \
    --cc=d.s@daniel.shahaf.name \
    --cc=turnbuckle@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).