Home Explore Blog CI



neovim

7th chunk of `runtime/doc/vimeval.txt`
f4afd5bb1b7956334e1e187e4068d648a6396c734cb1b7b70000000100000fa0
	:for key in sort(keys(mydict))

To loop over the values use the |values()| function:  >
	:for v in values(mydict)
	:   echo "value: " .. v
	:endfor

If you want both the key and the value use the |items()| function.  It returns
a List in which each item is a List with two items, the key and the value: >
	:for [key, value] in items(mydict)
	:   echo key .. ': ' .. value
	:endfor


Dictionary identity ~
							*dict-identity*
Just like Lists you need to use |copy()| and |deepcopy()| to make a copy of a
Dictionary.  Otherwise, assignment results in referring to the same
Dictionary: >
	:let onedict = {'a': 1, 'b': 2}
	:let adict = onedict
	:let adict['a'] = 11
	:echo onedict['a']
	11

Two Dictionaries compare equal if all the key-value pairs compare equal.  For
more info see |list-identity|.


Dictionary modification ~
							*dict-modification*
To change an already existing entry of a Dictionary, or to add a new entry,
use |:let| this way: >
	:let dict[4] = "four"
	:let dict['one'] = item

Removing an entry from a Dictionary is done with |remove()| or |:unlet|.
Three ways to remove the entry with key "aaa" from dict: >
	:let i = remove(dict, 'aaa')
	:unlet dict.aaa
	:unlet dict['aaa']

Merging a Dictionary with another is done with |extend()|: >
	:call extend(adict, bdict)
This extends adict with all entries from bdict.  Duplicate keys cause entries
in adict to be overwritten.  An optional third argument can change this.
Note that the order of entries in a Dictionary is irrelevant, thus don't
expect ":echo adict" to show the items from bdict after the older entries in
adict.

Weeding out entries from a Dictionary can be done with |filter()|: >
	:call filter(dict, 'v:val =~ "x"')
This removes all entries from "dict" with a value not matching 'x'.
This can also be used to remove all entries: >
	call filter(dict, 0)


Dictionary function ~
				*Dictionary-function* *self* *E725* *E862*
When a function is defined with the "dict" attribute it can be used in a
special way with a dictionary.  Example: >
	:function Mylen() dict
	:   return len(self.data)
	:endfunction
	:let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
	:echo mydict.len()

This is like a method in object oriented programming.  The entry in the
Dictionary is a |Funcref|.  The local variable "self" refers to the dictionary
the function was invoked from.

It is also possible to add a function without the "dict" attribute as a
Funcref to a Dictionary, but the "self" variable is not available then.

				*numbered-function* *anonymous-function*
To avoid the extra name for the function it can be defined and directly
assigned to a Dictionary in this way: >
	:let mydict = {'data': [0, 1, 2, 3]}
	:function mydict.len()
	:   return len(self.data)
	:endfunction
	:echo mydict.len()

The function will then get a number and the value of dict.len is a |Funcref|
that references this function.  The function can only be used through a
|Funcref|.  It will automatically be deleted when there is no |Funcref|
remaining that refers to it.

It is not necessary to use the "dict" attribute for a numbered function.

If you get an error for a numbered function, you can find out what it is with
a trick.  Assuming the function is 42, the command is: >
	:function g:42


Functions for Dictionaries ~
							*E715*
Functions that can be used with a Dictionary: >
	:if has_key(dict, 'foo')	" TRUE if dict has entry with key "foo"
	:if empty(dict)			" TRUE if dict is empty
	:let l = len(dict)		" number of items in dict
	:let big = max(dict)		" maximum value in dict
	:let small = min(dict)		" minimum value in dict
	:let xs = count(dict, 'x')	" count nr of times 'x' appears in dict
	:let s = string(dict)		" String representation of dict
	:call map(dict, '">> " .. v:val')  " prepend ">> " to each item


1.5 Blobs ~
						*blob* *Blob* *Blobs* *E978*
A Blob is a binary object.  It can be used to read an image from a file and
send it over a channel, for example.

A Blob mostly behaves like a |List| of

Title: Dictionary Manipulation: Identity, Modification, Functions, and Related Utilities
Summary
This section elaborates on dictionary manipulation in Vim script, covering aspects such as dictionary identity (requiring `copy()` or `deepcopy()` to avoid referencing the same object), modification using `:let`, and removal using `remove()` or `:unlet`. It also explains merging dictionaries with `extend()` and filtering entries using `filter()`. Additionally, the section introduces dictionary functions, enabling the use of functions with the `dict` attribute, and linking them to a dictionary. Finally, it lists utility functions such as `has_key()`, `empty()`, `len()`, `max()`, `min()`, `count()`, `string()`, and `map()` that can be used with dictionaries. It then briefly introduces Blobs as binary objects, similar to Lists.