Bash 间接引用关联数组

编程入门 行业动态 更新时间:2024-10-24 15:26:59
本文介绍了Bash 间接引用关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在这个非常简单的例子中,我需要同时处理数组元素的键和值:

In this very simplified example, I need to address both key and value of an array element:

declare -A writer writer[H.P.]=Lovecraft writer[Stephen]=King writer[Clive]=Barker writer[Jack]=Ketchum for i in ${!writer[@]} do echo "$i ${writer[$i]}" done fullname() { pointer=$1[@] for i in "${!pointer}" do echo "? $i" done } fullname writer

该函数必须以与之前的示例循环相同的格式显示输出,并且它应该接收数组名称、键或值列表,所有这些我都尝试过,但都没有成功.非常感谢任何建议.

The function must display the output in the same format as the example loop before it, and it should receive either array name, list of keys or values, all of which I tried, without success. Any suggestions are greatly appreciated.

推荐答案

indir_keys() { eval "echo ${!$1[@]}" } indir_val() { eval "echo ${$1[$2]}" } fullname() { pointer=$1 for i in $(indir_keys $pointer) do echo "$i $(indir_val $pointer $i)" done }

给予:

Jack Ketchum Clive Barker Stephen King H.P. Lovecraft

更多推荐

Bash 间接引用关联数组

本文发布于:2023-11-04 01:07:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!