Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Help with associative arrays
- X-seq: zsh-workers 39085
- From: Lawrence Velázquez <vq@xxxxxxxxx>
- To: acs@xxxxxxxxxxxxxxxxxxxx, ethersoft@xxxxxxxxx
- Subject: Re: Help with associative arrays
- Date: Sun, 21 Aug 2016 17:18:11 -0400
- Cc: zsh-workers@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=larryv.me; 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=7bZLlPi8UJ37oP80rFWNSVeBDkg=; b=d0MUvK 7r5FM/0iWXUDMS+aRZVYr8E1Zr9YrsgA8bqI8W0apU0NbQ7FotB2moRXMMF9Z/lV Al52FG8k2a7mow86e/yMHeYRyd7z+PwCEgtsAMbROoeF1uXOCN4ePs2rL2AZSl+m qwTQqYJQFM65aBNiCKiZR9VQRE1kAzd5g5nCo=
- 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=7bZLlPi8UJ37oP8 0rFWNSVeBDkg=; b=BGrlPkSMdX995UuwwDwN25lsIZ5WbXdraY4NKbbnNNb7Q35 OCHbtkJGLX8iYONbKYuMyFoG+lKZCEnbWsvZy5XxWKSfzAMatjJSqx9dySDBx3Mm tcU7yTjD3qmaqRyiglvX2yBdb4v2Vs5ikOdOu0M0ceYFVAuIwUbxdrET+7Zg=
- In-reply-to: <CACeGjnUfMEo5KtdMn=iiAVsfAJgjeVyk7vh+WFZgcyKeahBbnQ@mail.gmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CACeGjnUfMEo5KtdMn=iiAVsfAJgjeVyk7vh+WFZgcyKeahBbnQ@mail.gmail.com>
> On Aug 21, 2016, at 4:50 PM, Vin Shelton <ethersoft@xxxxxxxxx> wrote:
>
> I can't seem to figure this out. I'm trying to use an associative
> array and the results are not what I expect:
>
> #!/usr/bin/env zsh
>
> emulate -LR zsh
>
> typeset -A repos
> repos["conky"]="https://github.com/brndnmtthws/conky.git"
>
> nickname="conky"
> echo "nickname = " \"$nickname\" "repos = " \"${repos[$nickname]}\"
> echo "nickname = " \"$nickname\" "repos = " \"${repos["conky"]}\"
>
> yileds:
>
> nickname = "conky" repos = ""
> nickname = "conky" repos = "https://github.com/brndnmtthws/conky.git"
>
> given that nickname is "conky" I expected the values to be the same.
From zshparam(1):
The basic rule to remember when writing a subscript expression
is that all text between the opening `[' and the closing `]' is
interpreted as if it were in double quotes (see zshmisc(1)).
[...]
The second difference is that a double-quote (`"') may appear as
part of a subscript expression without being preceded by
a backslash....
The double quotes you're using are not acting as quoting syntax; they
are being treated literally and are becoming part of the key itself.
% typeset -A repos1; repos1["conky"]=foo; typeset repos1
repos1=( '"conky"' foo )
% typeset -A repos2; repos2[conky]=foo; typeset repos2
repos2=( conky foo )
But the quotes are behaving as you expect when you do the assignment to
"nickname".
% nickname="conky"; typeset nickname
nickname=conky
So $repos[$nickname] is evaluated as $repos[conky], not $repos["conky"],
while your array contains a '"conky"' key, not a 'conky' key. In the
end, you're not looking up the key you want.
tl;dr: Don't use double quotes in array subscripts, unless your array
contains keys with literal double quotes.
vq
Messages sorted by:
Reverse Date,
Date,
Thread,
Author