Skip to content

Segment: Set Values

William W. Kimball, Jr., MBA, MSIS edited this page May 31, 2021 · 3 revisions
  1. Introduction
    1. Accessing Set Values

Introduction

An unordered Set is very like an Array except for some important differences:

  1. An unordered Set will contain only unique values.
  2. The order of values within a Set is not guaranteed; any change to the Set will disrupt the ordinal representation of the Set when it is next accessed.
  3. Whereas an Array can hold any type of data as its elements, Set values are limited to a smaller selection of possible data types.

Sets are also represented differently than Arrays in both YAML and JSON. Some examples:

--- !!set
? Ring
? Necklace
? Bracelet

Note that a ? symbol is used for each Set value rather than an Array's - symbol for each of its elements. Note also that for Python (and thus, the YAML Path reference implementations of this project), the !!set tag must be used. Without it, the values will be internally recast as Hash keys with no values. While unrelated, JSON uses such a representation for Sets:

{ "Ring": null, "Necklace": null, "Bracelet": null }

This "looks like a Hash where all values are null" representation is simply because JSON has no formal representation for Sets.

Accessing Set Values

Because the position of each value within an unordered Set is not guaranteed, the values cannot be accessed by an index like Array elements. Instead, values are accessed only by their literal value. From the sample data above, to access the Necklace value, use: Necklace (dot notation) or /Necklace (forward-slash notation). Demarcation (" or ' pairs) and escape symbols (\) for special characters within a value is supported.

Clone this wiki locally