help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Organising conditions


From: hancooper
Subject: Re: Organising conditions
Date: Mon, 02 Aug 2021 19:34:19 +0000

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Monday, August 2, 2021 6:33 PM, Greg Wooledge <greg@wooledge.org> wrote:

> On Mon, Aug 02, 2021 at 06:17:43PM +0000, hancooper wrote:
>
> > declare -A aa=([a]="Joe" [b]="Peter" [c]="Sammy")
> > keyv ${aa[@]}
> > I get no output.
>
> unicorn:~$ keyv() {
> local -n _keyv_array="$1"
> local _keyv_key
> for _keyv_key in "${!_keyv_array[@]}"; do
> printf 'Key: %s\n' "$_keyv_key"
> printf 'Value: %s\n' "${_keyv_array[$_keyv_key]}"
> done
> }
> unicorn:~$ keyv aa
> Key: 1
> Value: 3
> Key: x
> Value: y
> unicorn:~$ declare -p aa
> declare -A aa=([1]="3" [x]="y" )
>
> And here's the dreaded name collision:
>
> unicorn:~$ keyv _keyv_array
> bash: local: warning: _keyv_array: circular name reference
> bash: warning: _keyv_array: circular name reference
> bash: warning: _keyv_array: circular name reference
>
> That's why I mangled the local variable names. It's done to protect
> against that, or worse.

I fail to follow what you are saying to me.

Doing

declare -A _keyv_array=([a]="Joe" [b]="Peter" [c]="Sammy")
keyv _keyv_array

does produce

Key:   a
Value: Joe
print
Key:   b
Value: Peter
print
Key:   c
Value: Sammy

The problem was that I was using ${aa[@]}






reply via email to

[Prev in Thread] Current Thread [Next in Thread]