[Top][All Lists]

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

Re: Syntax error in a Map/Hash initializer -- why isn't this supported?

From: Eli Schwartz
Subject: Re: Syntax error in a Map/Hash initializer -- why isn't this supported?
Date: Mon, 10 Aug 2020 18:44:02 -0400

On 8/10/20 5:56 PM, L A Walsh wrote:
> I wanted to use a map that looked like this:
> declare -A switches=([num]=(one two three)), where '(one two three)'
> is an associated list.  Ideally, I could access it like other arrays:
> for types in  ${switches[num][@]}; do...
> or
> switches[num]=(one two three)    #gives:
>                   -bash: switches[num]: cannot assign list to array member
> or
> echo ${switches[num][0]}  (="one").
> I defaulted to going around it by making it a string, like:
> switches[num]="one|two|three"

Correct, bash does not implement multidimensional arrays and its syntax
error if you try anyway, implements the minimum heuristics necessary to
convey that fact to you.

> or
> switches[num]="(one two three)" but why?  It seems obvious that bash
> knows what I'm trying to do, so why not just do it?

.. I'm sorry, did you just state your disapproval at bash for not
implementing something, but having a useful error message in the
process? Would you prefer "bash: error: an unknown error occurred"?

> Some nested constructs seem to work:
>> b=(1 2 3)
>> a=(4 5 6)
>> echo ${a[${b[1]}]}
> 6

This is not a multidimensional array, obviously. It is two
single-dimensional arrays, with a value lookup in one array that is then
reused as the input key to perform another lookup in the second array.

> but more often than not, they don't.  Is there a reason to disallow such?

Today's session of "Linda Walsh is year-by-year terrible at asking

Instead of asking:

"Can bash please implement multidimensional arrays as I think they're
nifty and would like to use them."

We are getting:

"Bash supports multidimensional arrays because $HACK but it doesn't let
me do them in X Y Z manner, is there a reason to arbitrarily forbid them
or can this pointless restriction be lifted."

This is a variant of http://catb.org/~esr/faqs/smart-questions.html#symptoms

Please read the whole article, but pay particular attention to the
section on "don't describe your problem in terms of the incorrect
diagnostic theory you've already decided is the real problem".

Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

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