如何将关联数组分配给zsh中的另一个变量?

时间:2020-12-20 00:02:37

In zsh, is there a way to assign an associative array to another variable? I would like to to something like this:

在zsh中,有没有办法将关联数组分配给另一个变量?我想这样的事情:

typeset -A orig
orig=(key1 val1 key2 val2)
typeset -A other
other=$orig
print '$orig:  '$orig
print '$other: '$other
print '$orig[key1]: '$orig[key1]
print '$other[key1]: '$other[key1]

This will print:

这将打印:

$orig:  val1 val2
$other: val1 val2
$orig[key1]: val1
$other[key1]: 

I want to be able to use $other[key1] and get val1.

我希望能够使用$ other [key1]并获得val1。

I know I can iterate over the keys and copy it item by item, but I really want to avoid this. Also, eval is evil :)

我知道我可以遍历键并逐项复制,但我真的想避免这种情况。另外,eval是邪恶的:)

I have tried other=($orig) and other variations, but this will get my values from orig and create as associative array like this

我已经尝试了其他=($ orig)和其他变体,但这将从orig获取我的值并创建为这样的关联数组

other=(val1 val2)

So other[key1] returns nothing and other[val1] returns val2, which is not what I want.

所以其他[key1]什么都不返回,其他[val1]返回val2,这不是我想要的。

If I understand correctly, what is going on in every attempt of mine is that $other is getting an array of the values of $orig, without the keys. How can I make it receive both keys and values and have the correct association between them?

如果我理解正确的话,我的每次尝试中发生的事情都是$ other获得$ orig值的数组,没有键。如何让它接收键和值并在它们之间保持正确的关联?

I'm not worried about null values, if that would even be a problem, because I am sure $orig will be well behaved.

我不担心空值,如果这甚至是一个问题,因为我相信$ orig将表现得很好。

Thanks!

谢谢!

2 个解决方案

#1


10  

You have to delve into the wonderful world of parameter expansion flags :) The k and v flags can be used together to force an associative array to expand to both its keys and values.

你必须深入研究参数扩展标志的精彩世界:) k和v标志可以一起使用来强制关联数组扩展到它的键和值。

$ typeset -A orig
$ orig=(key1 val1 key2 val2)
$ print ${(kv)orig}
key1 val1 key2 val2

Then you can use the set command to populate your copy with the alternating key/values produced by that expansion.

然后,您可以使用set命令使用该扩展生成的交替键/值填充副本。

$ typeset -A other
$ set -A other ${(kv)orig}
$ print $other[key1]
val1

These and other flags are documented in man zshexpn under "Parameter Expansion Flags", which is one of my favorite zsh features.

这些和其他标志记录在man zshexpn的“参数扩展标志”下,这是我最喜欢的zsh功能之一。

#2


3  

zsh: bad set of key/value pairs for associative array

zsh:关联数组的键/值对的错误集合

Perfect world without escaping:

没有逃脱的完美世界:

typeset -A old new
old=(k1 v1 k2 v2 k3 v3)
typeset old    # old=( k1 v1 k2 v2 k3 v3 )

...doesn't exist and your arrays usually include empty values:

...不存在,您的数组通常包含空值:

old[k2]=
typeset old    # old=( k1 v1 k2 '' k3 v3 )

...therefore you need to use " (quoting), @ (escaping) and f (field separation):

...因此你需要使用“(引用),@(转义)和f(场分离):

typeset new    # new=( )
new=("${(@fkv)old}")
typeset new    # new=( k1 v1 k2 '' k3 v3 )

See man zshexpn for more on parameter expansion flags.

有关参数扩展标志的更多信息,请参见man zshexpn。

#1


10  

You have to delve into the wonderful world of parameter expansion flags :) The k and v flags can be used together to force an associative array to expand to both its keys and values.

你必须深入研究参数扩展标志的精彩世界:) k和v标志可以一起使用来强制关联数组扩展到它的键和值。

$ typeset -A orig
$ orig=(key1 val1 key2 val2)
$ print ${(kv)orig}
key1 val1 key2 val2

Then you can use the set command to populate your copy with the alternating key/values produced by that expansion.

然后,您可以使用set命令使用该扩展生成的交替键/值填充副本。

$ typeset -A other
$ set -A other ${(kv)orig}
$ print $other[key1]
val1

These and other flags are documented in man zshexpn under "Parameter Expansion Flags", which is one of my favorite zsh features.

这些和其他标志记录在man zshexpn的“参数扩展标志”下,这是我最喜欢的zsh功能之一。

#2


3  

zsh: bad set of key/value pairs for associative array

zsh:关联数组的键/值对的错误集合

Perfect world without escaping:

没有逃脱的完美世界:

typeset -A old new
old=(k1 v1 k2 v2 k3 v3)
typeset old    # old=( k1 v1 k2 v2 k3 v3 )

...doesn't exist and your arrays usually include empty values:

...不存在,您的数组通常包含空值:

old[k2]=
typeset old    # old=( k1 v1 k2 '' k3 v3 )

...therefore you need to use " (quoting), @ (escaping) and f (field separation):

...因此你需要使用“(引用),@(转义)和f(场分离):

typeset new    # new=( )
new=("${(@fkv)old}")
typeset new    # new=( k1 v1 k2 '' k3 v3 )

See man zshexpn for more on parameter expansion flags.

有关参数扩展标志的更多信息,请参见man zshexpn。