Path: csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Greg Wooledge Newsgroups: gnu.bash.bug Subject: Re: Syntax error in a Map/Hash initializer -- why isn't this supported? Date: Tue, 11 Aug 2020 07:34:56 -0400 Lines: 43 Approved: bug-bash@gnu.org Message-ID: References: <8371B746-DDAB-48B1-932B-8197E3658255@larryv.me> <20200811113456.GJ931@eeg.ccf.org> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: usenet.stanford.edu 1597145727 6275 209.51.188.17 (11 Aug 2020 11:35:27 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org Envelope-to: bug-bash@gnu.org Mail-Followup-To: bug-bash@gnu.org Content-Disposition: inline In-Reply-To: <8371B746-DDAB-48B1-932B-8197E3658255@larryv.me> User-Agent: Mutt/1.10.1 (2018-07-13) Received-SPF: none client-ip=139.137.100.1; envelope-from=wooledg@eeg.ccf.org; helo=mail.eeg.ccf.org X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/11 07:34:56 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) BAYES_00=-1.9, KHOP_HELO_FCRDNS=1, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <20200811113456.GJ931@eeg.ccf.org> X-Mailman-Original-References: <8371B746-DDAB-48B1-932B-8197E3658255@larryv.me> Xref: csiph.com gnu.bash.bug:16738 > > "Can bash please implement multidimensional arrays as I think they're > > nifty and would like to use them." > It seems that Chet has never been interested, and no one else has > stepped up to contribute. A large reason for this is because bash is a shell, not a programming language. Another reason is that there are already ways to work around the "issue". The standard way to implement something that works like a multidimensional array is to use an associative array, and construct a key string out of your multiple indices, using some delimiter character that can't appear in any of the indices. For example, if your indices are integers, you could use a comma as the delimiter. Then: declare -A grid=( [0,0]=foo [0,1]=bar [0,2]=baz ) x=0 y=2 echo "${grid[$x,$y]}" This prints "baz" as expected. Another way which *only* works when your indices are small non-negative integers is to use a sparse indexed array, and construct a key index of the form i + (A)j + (A^2)k + ... where A is some suitably large constant chosen for your particular problem. For example, to store up to a 100x100 grid, we can choose A = 100, and let the individual indices run from 0 to 99. unset grid grid=( [0+100*0]=foo [0+100*1]=bar [0+100*2]=baz ) x=0 y=2 echo "${grid[x+100*y]}" This has the advantage of working in bash versions 2 and 3 which lack associative arrays, and the disadvantages of requiring numerical indices, and a strict up-front limit on the dimensions of your matrix. If you can't abide using "hacks" to implement your own multidimensional arrays, then bash isn't the right language for your project. Choose a different one.