# Set the `value` at `key`.

Values can then get retrieved with `[]`.

``````var x = new HashMap[String, Int]
x["four"] = 4
assert x["four"]   == 4``````

If the key was associated with a value, this old value is discarded and replaced with the new one.

``````x["four"] = 40
assert x["four"]         == 40
assert x.values.has(4)   == false``````

## Property definitions

##### core \$ Map :: []=
```	# Set the `value` at `key`.
#
# Values can then get retrieved with `[]`.
#
#     var x = new HashMap[String, Int]
#     x["four"] = 4
#     assert x["four"]   == 4
#
# If the key was associated with a value, this old value is discarded
# and replaced with the new one.
#
#     x["four"] = 40
#     assert x["four"]         == 40
#     assert x.values.has(4)   == false
#
fun []=(key: K, value: V) is abstract
```
lib/core/collection/abstract_collection.nit:711,2--726,38
##### counter \$ Counter :: []=
```	redef fun []=(e, value)
do
sum -= self[e]
self.map[e] = value
sum += value
end
```
lib/counter/counter.nit:63,2--68,4
##### trees \$ Trie :: []=
```	redef fun []=(key, value) do
var children = root.children

for i in [0..key.length[ do
var c = key.chars[i]

var node
if children.has_key(c) then
node = children[c]
else
node = new TrieNode[E](c)
children[c] = node
end
children = node.children

if i == key.length - 1 then
node.is_leaf = true
node.value = value
end
end
end
```
lib/trees/trie.nit:71,2--91,4
##### core \$ HashMap :: []=
```	redef fun []=(key, v)
do
if _capacity == 0 then enlarge(17) # 17 because magic in `store`
var i = index_at(key)
var c = node_at_idx(i, key)
if c != null then
c._key = key
c._value = v
else
store(i, new HashMapNode[K, V](key, v))
end
end
```
lib/core/collection/hash_collection.nit:252,2--263,4
##### trees \$ BinTreeMap :: []=
```	# Insert a new node in tree using `key` and `item`
# O(n) in worst case, average is O(h) with h: tree height
#
#     var tree = new BinTreeMap[Int, String]
#     tree = "n1"
#     assert tree.max == "n1"
#     tree = "n3"
#     assert tree.max == "n3"
redef fun []=(key, item) do
insert_node(new BinTreeNode[K, E](key, item))
end
```
lib/trees/bintree.nit:144,2--154,4
##### core \$ ArrayMap :: []=
```	# O(n)
redef fun []=(key, item)
do
var i = index(key)
if i >= 0 then
_items[i].second = item
else
_items.push(new Couple[K,E](key, item))
end
end
```
lib/core/collection/array.nit:705,2--714,4
##### trees \$ RBTreeMap :: []=
```	redef fun []=(key, item) do
insert_node(new RBTreeNode[K, E](key, item))
end
```
lib/trees/rbtree.nit:51,2--53,4
##### ini \$ IniFile :: []=
```	# Set the `value` for the property locaated at `key`
#
# ~~~
# var ini = new IniFile
# ini["key"] = "value1"
# ini["section1.key"] = "value2"
# ini["section2.key"] = "value3"
#
# assert ini["key"] == "value1"
# assert ini["section1.key"] == "value2"
# assert ini["section2.key"] == "value3"
# assert ini.section("section1").name == "section1"
# assert ini.section("section2")["key"] == "value3"
# ~~~
redef fun []=(key, value) do
if value == null then return
var parts = key.split_once_on(".")

# No dot notation, store value in root
if parts.length == 1 then
super(key.trim, value.trim)
return
end

# First part matches a section, store value in it
var section = self.section(parts.first.trim)
if section != null then
section[parts.last.trim] = value.trim
return
end

# No section matched, create a new one and store value in it
section = new IniSection(parts.first.trim)
section[parts.last.trim] = value.trim