How to access Dictionary keys with Variable

Create a Dictionary of Maps

maps = Dict(:Arad => Dict(:Zerinda => 75,:Sibiu => 140,:Timisora => 118))

Access Arad key works with the below code

maps[:Arad]

Access nested key works with the below code

maps[:Arad][:Sibiu]

Access key with getindex method work with the below code

getindex(maps,:Arad)

Access nested key with getindex works with the below code

getindex(getindex(maps,:Arad),:Sibiu)

Assign a variable

startpos = “Arad”

Here is my issue. How can i get key and nested key value using the variable ? few example I have issues.

getindex(maps,startpos)

maps.[startpos]

Please read PSA: how to quote code with backticks and update your post.

Use startpos = :Arad (which is the same you used when you used the key directly)

1 Like

Thanks it worked. Appreciate for your quick response.