Now do an associative array containing another associative array.
declare -A outer=(
[inner]="_inner"
)
declare -A _inner=(
[key]="value"
)
Access inner elements via a nameref. declare -n inner="${outer[inner]}"
echo "${inner[key]}"
# value
Currently writing a compiler in Bash built largely on this premise.Try in Python to make a nested defaultdict you can access like the following.
d = <something>
d["a"]["b"]["c"] # --> 42
Can't be done because it's impossible for user code to detect what the last __getitem__ call is and return the default.Edit: Dang it, I mean arbitrary depth.
c = defaultdict(lambda: 42)
b = defaultdict(lambda: c)
a = defaultdict(lambda: b)
a["a"]["b"]["c"] # --> 42Also d["a"] and d["a"]["b"] aren't 42.
d["a"]["b"]["c"] == (d["a"]["b"])["c"]
d = magicdict()
is42 = d["foo"]["bar"]["baz"]
# -> You can read any path and get a default if it doesn't exist.
d["hello"]["world"] = 420
# -> You can set any path and d will then contain { "hello": { "world": 420 }
People use things like jmespath to do this but the fundamental issue is that __getitem__ isn't None safe when you want nested dicts. It's a godsend when dealing with JSON.I feel like we're maybe too in the weeds, I should have just said "now have two expressions in your lambda."
# One
a = d["a"]["b"]["c"]
# Two
a = d["a"]["b"]
b = a["c"]I have a hard time understanding any use case outside of such mocking.
Otherwise I'm not sure of a mainstream language that would let you do a.get(x).get(y) == 42 but a.get(x).get(y).get(z) == 42, unless you resorted to monkey patching the number type, as it implies 42.get(z) == 42, which seems.. silly