bug-bash
[Top][All Lists]
Advanced

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

Re: Inconsistent behaviour of +=() and existing array keys


From: Eduardo A . Bustamante López
Subject: Re: Inconsistent behaviour of +=() and existing array keys
Date: Sat, 29 Nov 2014 03:47:12 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

On Fri, Nov 28, 2014 at 03:47:49PM -0500, Maarten Billemont wrote:
> What I would expect is for += inside +=() to behave as = does now, and = to
> behave as it does outside of +=(), which is to "set" the value, not
> append.  Ergo:
> 
> declare -a indexed_array=( [0]=a )
> indexed_array=( [0]=b ) #=> [0]=b  -- because we unset the array and set
> the element so those given.
> indexed_array+=( [0]=b ) #=> [0]=b -- because we mutate the array by
> setting the element to that given.
> indexed_array=( [0]+=b ) #=> [0]=b -- because we unset the array and set
> the element by appending the given to nothing.
> indexed_array+=( [0]+=b ) #=> [0]=bb -- because we mutate the array by
> appending the given to what is already there.

Further expanding on this:

| dualbus@hp ~ % cat array 
| #!/bin/bash
| 
| for shell in \
|     ksh93 \
|     ~/local/bin/bash \
|     /tmp/bash/bin/bash; do
|     echo "$shell"
|     "$shell" -c '
|     typeset -A A
|     A=( ["k"]=a )
|      echo "A=([\"k\"]=b)"
|      A=(["k"]=b);  typeset -p A
|     A=( ["k"]=a )
|     echo "A+=([\"k\"]=b)"
|     A+=(["k"]=b);  typeset -p A
|     A=( ["k"]=a )
|      echo "A=([\"k\"]+=b)"
|      A=(["k"]+=b); typeset -p A
|     A=( ["k"]=a )
|     echo "A+=([\"k\"]+=b)"
|     A+=(["k"]+=b); typeset -p A
| '
| done


| dualbus@hp ~ % ./array                               
| ksh93
| A=(["k"]=b)
| typeset -A A=([k]=b)
| A+=(["k"]=b)
| typeset -A A=([k]=b)
| A=(["k"]+=b)
| typeset -A A=([k]=b)
| A+=(["k"]+=b)
| typeset -A A=([k]=ab)
| /home/dualbus/local/bin/bash
| A=(["k"]=b)
| declare -A A='([k]="b" )'
| A+=(["k"]=b)
| declare -A A='([k]="ab" )'
| A=(["k"]+=b)
| declare -A A='([k]="b" )'
| A+=(["k"]+=b)
| declare -A A='([k]="ab" )'
| /tmp/bash/bin/bash
| A=(["k"]=b)
| declare -A A='([k]="b" )'
| A+=(["k"]=b)
| declare -A A='([k]="b" )'
| A=(["k"]+=b)
| declare -A A='([k]="b" )'
| A+=(["k"]+=b)
| declare -A A='([k]="ab" )'


~/local/bin/bash is bash from the devel branch. /tmp/bash/bin/bash is bash with
the attached patch applied. The attached patch makes bash behave like ksh93, at
least for the associative arrays case, because in the case of normal arrays,
ksh93 does some pretty funny things:

| dualbus@hp ~ % ksh93 -c 'a=(); a+=([0]=b); typeset -p a'
| typeset -A a=([0]=b) # Ha! Now 'a' is an associative array...

Attachment: arrayfunc.patch
Description: Text Data


reply via email to

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