zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: Passing array to function
Date: Fri, 30 Sep 2016 11:26:15 -0700	[thread overview]
Message-ID: <160930112615.ZM16681@torch.brasslantern.com> (raw)
In-Reply-To: <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.


  parent reply	other threads:[~2016-09-30 18:34 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
2016-09-30 18:26   ` Bart Schaefer [this message]
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=160930112615.ZM16681@torch.brasslantern.com \
    --to=schaefer@brasslantern.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).