* Help with associative arrays
@ 2016-08-21 20:50 Vin Shelton
2016-08-21 21:18 ` Lawrence Velázquez
0 siblings, 1 reply; 4+ messages in thread
From: Vin Shelton @ 2016-08-21 20:50 UTC (permalink / raw)
To: Zsh Hackers' List
[-- Attachment #1: Type: text/plain, Size: 577 bytes --]
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.
Puzzled,
Vin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Help with associative arrays
2016-08-21 20:50 Help with associative arrays Vin Shelton
@ 2016-08-21 21:18 ` Lawrence Velázquez
2016-08-22 11:53 ` Vin Shelton
2016-08-23 15:54 ` Stephane Chazelas
0 siblings, 2 replies; 4+ messages in thread
From: Lawrence Velázquez @ 2016-08-21 21:18 UTC (permalink / raw)
To: acs, ethersoft; +Cc: zsh-workers
> On Aug 21, 2016, at 4:50 PM, Vin Shelton <ethersoft@gmail.com> 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Help with associative arrays
2016-08-21 21:18 ` Lawrence Velázquez
@ 2016-08-22 11:53 ` Vin Shelton
2016-08-23 15:54 ` Stephane Chazelas
1 sibling, 0 replies; 4+ messages in thread
From: Vin Shelton @ 2016-08-22 11:53 UTC (permalink / raw)
To: Lawrence Velázquez; +Cc: Zsh Hackers' List
[-- Attachment #1: Type: text/plain, Size: 316 bytes --]
I will quote only the summary, but the explanation was well worthwhile.
On Sun, Aug 21, 2016 at 5:18 PM, Lawrence Velázquez <vq@larryv.me> wrote:
> tl;dr: Don't use double quotes in array subscripts, unless your array
> contains keys with literal double quotes.
>
Yes, thank you, that works.
- Vin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Help with associative arrays
2016-08-21 21:18 ` Lawrence Velázquez
2016-08-22 11:53 ` Vin Shelton
@ 2016-08-23 15:54 ` Stephane Chazelas
1 sibling, 0 replies; 4+ messages in thread
From: Stephane Chazelas @ 2016-08-23 15:54 UTC (permalink / raw)
To: Lawrence Velázquez; +Cc: acs, ethersoft, zsh-workers
2016-08-21 17:18:11 -0400, Lawrence Velázquez:
[...]
> The second difference is that a double-quote (`"') may appear as
> part of a subscript expression without being preceded by
> a backslash....
[...]
An annoying consequence is that (contrary to with ksh93) you can't do:
a[""]=foo
To assign an element for the empty key. You have to do things
like:
a[${}]=foo
or
a+=('' foo)
instead.
It would be nice if one could do:
a[]=foo
(which currently returns an error)
Note that it's a lot worse in bash. bash associative arrays
can't have an element with an empty key!
--
Stephane
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-08-23 15:54 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-21 20:50 Help with associative arrays Vin Shelton
2016-08-21 21:18 ` Lawrence Velázquez
2016-08-22 11:53 ` Vin Shelton
2016-08-23 15:54 ` Stephane Chazelas
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).