Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #15185 > unrolled thread

Re: Unexpected result of array assignment

Started byL A Walsh <bash@tlinx.org>
First post2019-07-17 22:46 -0700
Last post2019-07-17 22:46 -0700
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Unexpected result of array assignment L A Walsh <bash@tlinx.org> - 2019-07-17 22:46 -0700

#15185 — Re: Unexpected result of array assignment

FromL A Walsh <bash@tlinx.org>
Date2019-07-17 22:46 -0700
SubjectRe: Unexpected result of array assignment
Message-ID<mailman.1622.1563428772.2688.bug-bash@gnu.org>
On 2019/07/17 18:16, Darren 'Tadgy' Austin wrote:
> Repeat-By:
>         declare -A foo
>         foo=(["key"]="value1")
>         declare -p foo
>         foo=(["key"]="${foo["key"]} value2")
>         declare -p foo
>
>         The above should result in 'foo["key"]' having a value of 'value1 value2', but the result is simply ' value2', which I believe to be incorrect behaviour.
>   

In bash4.4.12, Using:

I think you need to tell bask that you are updating 'foo'
instead of assigning to it:

This seems to do what you want:

foo+=([key]="${foo[key]} value2")
> my -p foo
declare -A foo=([key]="value1 value2" )

or w/quotes:
foo+=(["key"]="${foo["key"]} value3")

> my -p foo
declare -A foo=([key]="value1 value2 value3" )

I think that without the update it becomes an assign and clears
the value assigned to 'key' before using it to form the string.

It certainly isn't intuitive, but I don't know if there is a
guarantee of it picking up the value of "foo[key]" before initializing
the target space.



[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web