From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10457 invoked by alias); 2 Nov 2015 16:28:17 -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: 20875 Received: (qmail 11449 invoked from network); 2 Nov 2015 16:28:15 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 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:content-type; bh=PYSYwYrL3etbfKe3anT2G0QZ0cSX7JnLBDUiCaLH2ro=; b=i50AWQzVhPyn7wb6vY9tL7cHahp9S7FF+/GQ/r93Y54ApV7eEeq1dNdsIWMCgyRY3D 7hsYoII+b5WJbZFRkarC4YiIwwEF8ehJjkGZdsBQq+bhuDEXwOpaoAYMpw3AbMotdAnE RkZ9X8QEvgx8+ly7kElQ5c1vEBhHnn8RMcVZ5JpvdyDmHuHqNtd4yEqa748J9PSub7+r wI3cXbxXsKlh6on17ywrTfHM6hZcie06vw7/vwdX3SxrE0YvYsXOhiD3529mp6LE2TbO hOLUfFNX9b5HJJxC711/Uea06jG+8SAbEwqFJnd8+SsskmIIXlCFCC0+Y76P0g7DfIkA yCDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:to:subject:mime-version:content-type; bh=PYSYwYrL3etbfKe3anT2G0QZ0cSX7JnLBDUiCaLH2ro=; b=DXufCIId9MgzTItZOFN6R66ZN/QtfK2GlBn/UtvNFu7oFmdRy+oPMznL0+nqi71g/N EPC0h9pE60TQkHtQHV4V2a3TugXj3VE9Xaa2edxOx4brvtTLek2ZPrCVFY9XueeCGsYG oZEhv3wYtYdLptmsea0GD5OYhCJI0RIRkIFJfOWru1UV7gOdfPB0WAvZmGuJnYVb+JTK A42Ka3XBnhMVGd99P5pO8p8s6ud0zKLT7ufwT+kMVUge1fn2LTdmIq5DWEpWAnL0yggT u0hYeAMLJ1A/X0aPcKGRL73o5Uk4KfPeFJPbHj96p0G5XcQZd/wQ67/zd2Z8h+L3CK1h SQsQ== X-Gm-Message-State: ALoCoQmpBBSo+o851Xfhw5QWIeoj+/Q5+B5bjBo8xQTYA/CQyf5Ae6NzQHYgWgZ0vsVCdhG0x/0o X-Received: by 10.182.88.165 with SMTP id bh5mr14833420obb.21.1446481690753; Mon, 02 Nov 2015 08:28:10 -0800 (PST) From: Bart Schaefer Message-Id: <151102082808.ZM17640@torch.brasslantern.com> Date: Mon, 2 Nov 2015 08:28:08 -0800 In-Reply-To: <563784B5.3040901@eastlink.ca> Comments: In reply to Ray Andrews "Re: easy calling of associative array?" (Nov 2, 7:43am) References: <56369C7B.2030604@eastlink.ca> <1237641446422150@web6m.yandex.ru> <5636B333.8060300@eastlink.ca> <151101190842.ZM16752@torch.brasslantern.com> <5636D99F.2030807@eastlink.ca> <151101225100.ZM16882@torch.brasslantern.com> <563784B5.3040901@eastlink.ca> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: easy calling of associative array? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Nov 2, 7:43am, Ray Andrews wrote: } } get_v () } { } #$3=${(P)${:-${1}[$2]}} #nothing works } eval "$3=\$${1}[$2]" #this works } } This has nothing to do with associative arrays and everything to do with assignment syntax. "$3" is not a valid identifier, so "$3=" is not an assignment; this all decided before $3 is expanded, which is why putting the "eval" around it works (it delays the "is an identifier" check until after $3 has expanded). Probably the most cogent way to do this is get_v () { typeset -g $3="${(P)${:-${1}[$2]}}" } } set_v () } { } # {(P)${:-${1}[$2]}}=$3 #nothing works } eval "${1}[$2]=$3" #this works } } Same assignment-syntax problem. set_v () { # typeset -gA $1 # optional typeset -g "${1}[$2]=$3" # quotes so [ ] isn't globbing } Here you don't need the (P) indirection because ${1} and $2 are both expanded before being passed to typeset, so you already extracted the name that was passed in $1. Also note I'm ignoring all possible error checking, e.g. if $1 is not an identifier (in the worst case, contains an "="), things go badly. } test_v () } { } #eval "${1}[$2]" #nothing works } #this works } [ ${(P)${:-${1}[$2]}} = $3 ] && echo 'all too true' || echo 'alas, no.' } } I'm not exactly sure what you're wanting as either output or exit status here, but except that I'd recommend [[ ]] instead of [ ] as the test syntax, what you wrote for "this works" is sensible.