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.
3.4.2 Hashes
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.
3.4.2.1 The Dictionary
The Erlang dict
module implements a key/value dictionary part of which is
an additional dict
data type which supplements the built-in Erlang data
types.
Here's how you create a new dict
:
Let's check that there's no actual data in it:
Now let's add some!
As you might guess from the usage, dict
s are not updated in-place. A new
dictionary is returned with each call to append
. As such, we need to
reset
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 dict
:
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 |to see|
key now has two values in it.
You can also build dict
s from a list of tuples:
There are many more functions to explore in the dict docs.
3.4.2.2 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 ets
module
supports hash tables of the following types:
* set
* ordered_set
* bag
* duplicate_bag
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).