3 Lists and Simple Data
3.4 Property Lists and Hashes
3.4.1 Property Lists
Property lists are just lists whose entries are key/value tuples.
Alternatively, an entry may be a single atom, in which case it implies a tuple
with the atom as the key and
true as the value.
Since there's no special type here, we just create a regular list:
Let's see what keys we have defined:
Extracting data by key:
If you know that your value is single-valued (e.g., not a list), then you can do this:
There is more information about property lists on the docs page for them.
There is no builtin "dictionary" or "hash" type in Erlang. However, there are some libraries that support data structures like these. There is also a concept of "records" which we will discuss in another section.
18.104.22.168 The Dictionary
dict module implements a key/value dictionary part of which is
dict data type which supplements the built-in Erlang data
Here's how you create a new
Let's check that there's no actual data in it:
Now let's add some!
As you might guess from the usage,
dicts are not updated in-place. A new
dictionary is returned with each call to
append. As such, we need to
set with each append.
Is everything there?
Looking good so far... Now let's get some data out:
Why the is the function called "append"? Well,
dict accepts multiple
values for keys. Let's try this out, and then re-query our
The size of the
my-dict didn't change because we didn't add a new key;
rather, we updated an existing one, appending a new value. The
key now has two values in it.
You can also build
dicts from a list of tuples:
There are many more functions to explore in the dict docs.
22.214.171.124 Other Hash Tables
OTP comes with the
ets module which provides the ability to store very
large quantities of data in an Erlang runtime system. The
supports hash tables of the following types:
The documentation for this module is here, though we will be adding information on how to use this from LFE at a later point (likely a dedicated tutorial).