Skip to main content

All Questions

Tagged with
1 vote
1 answer
2k views

How to retrieve items from an array of arrays?

Hello StackExchange pros! I am working on a zsh project for macOS. I used typeset to create three associative arrays to hold values, and a fourth array to reference the individual arrays. Is it ...
jamesrg71's user avatar
5 votes
2 answers
2k views

Set a key with spaces in an associative array in Zsh

In Bash (4 or above), if I have an associative array dict, I can set its value like dict[apple count]=1 and I would be able to access it with ${dict[apple count]}. Does Zsh allow space in key names? ...
Franklin Yu's user avatar
  • 1,269
19 votes
1 answer
2k views

How to use associative arrays safely inside arithmetic expressions?

A few Bourne-like shells support associative arrays: ksh93 (since 1993), zsh (since 1998), bash (since 2009), though with some differences in behaviour between the 3. A common use is for counting ...
Stéphane Chazelas's user avatar
10 votes
1 answer
2k views

In ZSH, how do I unset an arbitrary associative array element?

An associative array I have has arbitrary keys, including keys containing backquotes, brackets, etc: $ typeset -A arr $ key='`' $ arr[$key]=backquote $ echo $arr[$key] backquote I now need to unset ...
Michaël's user avatar
  • 834
4 votes
4 answers
2k views

zsh testing existence of a key in an associative array via indirect expansion

So I know that you can test for the existence of a regular parameter via indirect expansion by doing something like: foo=1 bar=foo (( ${(P)+bar} )) && print "$bar exists" And I know you can ...
Matt Wilder's user avatar
2 votes
2 answers
2k views

no matches found when using associative arrays in zsh

I am encountering no matches found when using map in zsh: #!/bin/zsh declare -A map=(["8761"]="Eureka服务" ["11001"]="用户微服务") Why would this happen, and how can I fix it? This is the error: ~/source/...
Dolphin's user avatar
  • 761
10 votes
4 answers
2k views

Inverting an associative array

Let's say I have an associative array in bash, declare -A hash hash=( ["foo"]=aa ["bar"]=bb ["baz"]=aa ["quux"]=bb ["wibble"]=cc ["wobble"]=aa ) where both keys and values are ...
Kusalananda's user avatar
  • 351k
1 vote
0 answers
815 views

zsh parameter expansion flag (P) with associative arrays

In the zshexpn manual page (zsh version 5.1.1) we have: Parameter Expansion Flags (P): This forces the value of the parameter name to be interpreted as a further parameter name, whose value will be ...
Zorawar's user avatar
  • 855