Maps & Hashes
Maps (also known as Hashes or Dictionaries) store associative key-value pairs. They support fast lookups, arbitrary key types, nested structures, and convenient built-in methods.
Creating & Modifying Maps
djazair
let user = {
"name": "Riyadh",
"role": "Developer",
"level": 5,
"skills": ["C", "Djazair", "Web"]
}
# Accessing keys
print(user["name"]) # => Riyadh
# Updating & Adding keys
user["level"] += 1 # level is now 6
user["city"] = "Blida" # new key added
# Checking key existence
print("role" in user) # => True
print(user.has("salary")) # => False
Map Methods Catalog
| Method | Description | Example |
|---|---|---|
length() | Number of key-value pairs | user.length() |
has(key) | Returns True if key is present | user.has("name") |
get(key) | Retrieves value of key (Null if missing) | user.get("missing") |
keys() | Returns an Array of all map keys | user.keys() |
values() | Returns an Array of all map values | user.values() |
items() | Returns array of [key, value] pairs | user.items() |
update(otherMap) | Merges another map into this map | user.update({"active": True}) |
setDefault(k, v) | Sets key to default value if missing | user.setDefault("theme", "dark") |
pop(key) | Removes and returns value of key | user.pop("level") |
clear() | Removes all entries from map | user.clear() |
copy() | Creates a shallow copy of the map | let clone = user.copy() |
Iterating Over Maps
The for-in loop directly supports key-value destructuring:
djazair
let config = {
"host": "localhost",
"port": 8080,
"debug": True
}
for key, value in config
print("${key} => ${value}")
end
# host => localhost
# port => 8080
# debug => True