-
-
Notifications
You must be signed in to change notification settings - Fork 18
MetaData
Bash associative arrays are inherently one-dimensional. However, by using composite keys, we can simulate a two-dimensional array. This allows us to structure and access data in a way that mimics a 2D array.
In a true 2D array, elements are accessed using two indices. Bash’s associative arrays only support one dimension natively, so we simulate a 2D array by combining multiple pieces of information into a single key.
module_options+=(
["section,foo"]="value for foo"
["section,bar"]="value for bar"
)
-
Key Format:
["section,identifier"]
-
Simulated Dimensions:
-
First Dimension:
section
(e.g.,foo
,bar
) -
Second Dimension:
identifier
(e.g.,foo
,bar
)
-
First Dimension:
For specific metadata:
module_options+=(
["is_package_manager_running,author"]="Joey Turner"
["is_package_manager_running,status"]="Active"
)
-
Key Format:
["identifier,attribute"]
-
Simulated Dimensions:
-
First Dimension:
identifier
(e.g.,is_package_manager_running
) -
Second Dimension:
attribute
(e.g.,author
,status
)
-
First Dimension:
To retrieve a value:
echo "${module_options["is_package_manager_running,author"]}" # Outputs: Joey Turner
-
Composite Keys: By combining
identifier
andattribute
into a single key, we simulate a 2D array where:- The first dimension is represented by the
identifier
. - The second dimension is represented by the
attribute
.
- The first dimension is represented by the
-
Values: Are associated with these composite keys to allow organized data retrieval.
- Structured Data: Mimics a 2D array's organization.
- Efficient Retrieval: Allows structured access to values using composite keys.
- Not Truly 2D: This is still a 1D associative array with composite keys.
- Complexity: Managing composite keys can be more complex compared to native multidimensional arrays.
Using composite keys in an associative array simulates a 2D array structure within the constraints of Bash’s 1D associative arrays, providing a way to organize and access data effectively.