Maps in Julia
To create an empty map, use the Dict
constructor:
Set key/value pairs using typical name[key] = val
syntax.
Printing a map using println
will show all of its key/value pairs.
Get a value for a key with name[key]
.
If the key doesn’t exist, trying to access it will result in a KeyError
. You can avoid this by using the get
function.
The length
function returns the number of key/value pairs in the map.
Use the delete!
function to remove key/value pairs from a map.
Clear all key/value pairs from the map using the empty!
function.
Use the haskey
function to check if a key is present in the map. This can be used to disambiguate between missing keys and keys with zero values like 0
or an empty string.
You can also declare and initialize a new map in the same line with this syntax.
Note that maps appear in the form Dict(k => v, k => v)
when printed with println
.
Next example: Functions