From 7fb2f79c4bf0eee728543e0cdc6a0bf4d37cf1d0 Mon Sep 17 00:00:00 2001 From: koalaman Date: Sat, 6 Aug 2016 18:44:56 -0700 Subject: [PATCH] Created SC2180 (markdown) --- SC2180.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 SC2180.md diff --git a/SC2180.md b/SC2180.md new file mode 100644 index 0000000..c5fae9c --- /dev/null +++ b/SC2180.md @@ -0,0 +1,32 @@ +## Bash does not support multidimensional arrays. Use 1D or associative arrays. + +### Problematic code: + +```sh +foo[1][2]=bar +echo "${foo[1][2]}" +``` + +### Correct code: + +In bash4, consider using associative arrays: +```sh +declare -A foo +foo[1,2]=bar +echo "${foo[1,2]}" +``` + +Otherwise, do your own index arithmetic: +```sh +size=10 +foo[1*size+2]=bar +echo "${foo[1*size+2]}" +``` + +### Rationale: + +Bash does not support multidimensional arrays. Rewrite it to use 1D arrays. Associative arrays map arbitrary strings to values, and are therefore useful since you can construct keys like `"1,2,3"` or `"val1;val2;val3"` to index them. + +### Exceptions: + +None. \ No newline at end of file