Sidechair

map.put()

Browse the reference

Stores a value under a key, adding the pair or replacing the old value, and returns the previous value.

Try it in the editor

If the key is new, a pair is added at the end of the map's order and the call returns na. If the key exists, its value is replaced in place and the old value is returned.

Adding a pair that would take the map past 50,000 pairs stops the script with an error.

Syntax

Pine Script
map.put(id: any map type, key: series <type of the map's elements>, value: series <type of the map's elements>) -> <value_type>
map.put(key: series <type of the map's elements>, value: series <type of the map's elements>) -> <value_type>

Arguments

NameTypeDefaultDescription
idany map typerequiredThe map that stores the pair. A key that is already there gets the new value.
keyseries <type of the map's elements>requiredThe key to look up or store. It has to match the key type of the map.
valueseries <type of the map's elements>requiredThe value to store. It has to match the value type of the map.

Returns

<value_type>: The value that was stored under the key before, or na when the key was new.

Remarks

  • The return value is easy to ignore; map.put(m, k, v) works as a plain statement.

Example

Pine ScriptRemember the last close per hourOpen in editor
//@version=6
indicator("map.put: last close by hour", overlay = false)
var lastClose = map.new<int, float>()
prev = map.put(lastClose, hour, close)
plot(close - nz(prev, close), "Change since this hour last time")

See also

Runs on Sidechair