Function File: dict ()
Function File: dict (keys, values)
Function File: dict (str)

Creates a dictionary object with given keys and values.

The class dict has been deprecated in favour of using Octave structs. The advantage of dict over structs was that dict allowed any string, not only valid Octave identifiers, as fieldnames. This has since change and any string is now a valid fieldname.

s = struct ("7", "value 7", "  ", "just spaces");
s.("7")
  ⇒ ans = value 7
s.("  ")
  ⇒ ans = just spaces

keys should be a cell array of strings; values should be a cell array with matching size. values can also be a singleton array, in which case it is expanded to the proper size; or omitted, in which case the default value of empty matrix is used. If neither keys nor values are supplied, an empty dictionary is constructed. If a scalar structure is supplied as an argument, it is converted to a dictionary using field names as keys.

A dictionary can be indexed either by a single string or cell array of strings, like this:

  d = dict (keys, values);
  d(str) # result is a single value
  d(cellstr) # result is a cell array

In the first case, the stored value is returned directly; in the second case, a cell array is returned. The cell array returned inherits the shape of the index.

Similarly, indexed assignment works like this:

  d = dict (keys, values);
  d(str) = val; # store a single value
  d(cellstr) = vals; # store a cell array
  d(cellstr) = []; # delete a range of keys

Any keys that are not present in the dictionary are added. The values of existing keys are overwritten. In the second case, the lengths of index and rhs should match or rhs should be a singleton array, in which case it is broadcasted.

It is also possible to retrieve keys and values as cell arrays, using the "keys" and "values" properties. These properties are read-only.

Package: general