Discussion:
Assignment of associative arrays through braces
konsolebox
2018-07-27 23:13:41 UTC
Permalink
Hi Chet,

I wonder if you can allow bash to have another syntax to allow simpler
declaration and/or definition of associative arrays. The changes
needed to have it done seem simple enough, and the only conflict it
makes is a scalar `var={...` assignment, which in my opinion is better
quoted to make it more readable and less questionable from other
syntaxes like brace expansion. I believe most people intuitively
quotes it, and assignments that start with `{` is fairly rare.

I made a patch for this which I include as an attachment. I also
uploaded the source tree to
https://github.com/konsolebox/bash/tree/assoc_array_brace_assign.

Besides having an easy way to define associative arrays, this feature
would also allow associative arrays to be used as "return variables".
The caller wouldn't have to initialize the associative array unless it
wants to have it declared local for itself, and the callee that
returns the data can allow itself to be a complete and independent
function.

g() { a={[x]=y}; }
f() { local a={}; g; declare -p a; }
a={[a]=b}; f; declare -p a

Output:

declare -A a=([x]="y" )
declare -A a=([a]="b" )
--
konsolebox
Chet Ramey
2018-07-31 14:31:25 UTC
Permalink
Post by konsolebox
Hi Chet,
I wonder if you can allow bash to have another syntax to allow simpler
declaration and/or definition of associative arrays. The changes
needed to have it done seem simple enough, and the only conflict it
makes is a scalar `var={...` assignment, which in my opinion is better
quoted to make it more readable and less questionable from other
syntaxes like brace expansion. I believe most people intuitively
quotes it, and assignments that start with `{` is fairly rare.
So it's syntactic sugar for `declare -gA a; a=( ... )'?
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://tiswww.cwru.edu/~chet/
konsolebox
2018-07-31 16:53:38 UTC
Permalink
Post by Chet Ramey
Post by konsolebox
Hi Chet,
I wonder if you can allow bash to have another syntax to allow simpler
declaration and/or definition of associative arrays. The changes
needed to have it done seem simple enough, and the only conflict it
makes is a scalar `var={...` assignment, which in my opinion is better
quoted to make it more readable and less questionable from other
syntaxes like brace expansion. I believe most people intuitively
quotes it, and assignments that start with `{` is fairly rare.
So it's syntactic sugar for `declare -gA a; a=( ... )'?
That surely is one of the main goals, but it's not exact. `declare
-gA a; a=(...)` would always affect the main global scope.

Example:

$ g() { declare -gA a=([x]=y); }; f() { local -A a=(); g; declare -p
a; }; declare -A a=([a]=b); f; declare -p a
declare -A a=()
declare -A a=([x]="y" )

But as shown in my earlier example, a={...} would only affect the
nearest scope which was local a={}.

This proposal simply requests an intuitively equivalent simple
assignment syntax for associative arrays just like a='...' and
a=(...), with same behavior for scoping.

Maybe we can add another option like -G to have similar effect but
that's a little different already. Just allowing a={} would make it
simpler for every scripter.
--
konsolebox
konsolebox
2018-08-19 14:27:31 UTC
Permalink
Post by konsolebox
Post by Chet Ramey
Post by konsolebox
Hi Chet,
I wonder if you can allow bash to have another syntax to allow simpler
declaration and/or definition of associative arrays. The changes
needed to have it done seem simple enough, and the only conflict it
makes is a scalar `var={...` assignment, which in my opinion is better
quoted to make it more readable and less questionable from other
syntaxes like brace expansion. I believe most people intuitively
quotes it, and assignments that start with `{` is fairly rare.
So it's syntactic sugar for `declare -gA a; a=( ... )'?
That surely is one of the main goals, but it's not exact. `declare
-gA a; a=(...)` would always affect the main global scope.
$ g() { declare -gA a=([x]=y); }; f() { local -A a=(); g; declare -p
a; }; declare -A a=([a]=b); f; declare -p a
declare -A a=()
declare -A a=([x]="y" )
But as shown in my earlier example, a={...} would only affect the
nearest scope which was local a={}.
This proposal simply requests an intuitively equivalent simple
assignment syntax for associative arrays just like a='...' and
a=(...), with same behavior for scoping.
Maybe we can add another option like -G to have similar effect but
that's a little different already. Just allowing a={} would make it
simpler for every scripter.
By the way, is this rejected?
--
konsolebox
Chet Ramey
2018-08-20 18:20:35 UTC
Permalink
Post by konsolebox
Post by konsolebox
This proposal simply requests an intuitively equivalent simple
assignment syntax for associative arrays just like a='...' and
a=(...), with same behavior for scoping.
Maybe we can add another option like -G to have similar effect but
that's a little different already. Just allowing a={} would make it
simpler for every scripter.
By the way, is this rejected?
No, it will not make it into bash-5.0 so I haven't looked at it further.
It is on the list for a future release.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://tiswww.cwru.edu/~chet/
konsolebox
2018-08-21 07:39:00 UTC
Permalink
Post by Chet Ramey
Post by konsolebox
Post by konsolebox
This proposal simply requests an intuitively equivalent simple
assignment syntax for associative arrays just like a='...' and
a=(...), with same behavior for scoping.
Maybe we can add another option like -G to have similar effect but
that's a little different already. Just allowing a={} would make it
simpler for every scripter.
By the way, is this rejected?
No, it will not make it into bash-5.0 so I haven't looked at it further.
It is on the list for a future release.
Ok, sounds good.
--
konsolebox
Loading...