Title here
Summary here
Explanation:
var m = initTable[string, int]()
initializes an empty map where the keys are of type string
and the values are of type int
.m["k1"] = 7
and m["k2"] = 13
set key-value pairs in the map.echo "map: ", m
prints the entire map showing all its key-value pairs.let v1 = m["k1"]
retrieves the value for the key "k1"
.let v3 = m.getOrDefault("k3", 0)
retrieves the value for the key "k3"
or returns 0
if the key does not exist.m.len
returns the number of key-value pairs in the map.m.del("k2")
removes the key-value pair for the key "k2"
.m.clear()
removes all key-value pairs from the map.m.hasKey("k2")
checks if the key "k2"
exists in the map.var n = initTable[string, int]({"foo": 1, "bar": 2})
declares and initializes a new map.let n2 = initTable[string, int]({"foo": 1, "bar": 2})
declares and initializes another map.if n == n2
checks if the two maps are equal.To run the program, save the code in a file with a .nim
extension and run it using the Nim compiler:
Here’s the expected output:
Next example: Functions.