A sequence:
- Is ordered.
- Known as a list or array in other languages.
- Looks like a bullet list.
Here is an example of a sequence:
- 1
- 2
- 3
- 4
- 5The value of each item is a number.
Here is another example of a sequence with string values:
- A
- B
- C
- D
- EHere is another sequence with mixed values:
- 1
- B
- C
- 4
- EA scalar value can map to a sequence. This is common with a one-to-many data relationship. The following example demonstrates this by mapping the family of fruit to various specific types of fruit.
citrus:
- orange
- lemon
- lime
stone:
- peach
- plum
- cherryThe indentation isn't required when mapping scalars to sequences.
This is the exact same value as shown above.
citrus:
- orange
- lemon
- lime
stone:
- peach
- plum
- cherryIndent each line to at least the level of the mapping key.
The following example would be wrong because the sequence isn't part of the citrus map.
citrus:
- orange
- lemon
- lime citrus:
- orange
- lemon
- lime citrus:
- orange
- lemon
- limeRegardless of your indentation preference, keep the sequence items (starting with -) at the same alignment.
citrus:
- orange
- lemon
- limecitrus:
- orange
- lemon
- limeSequences can be written in a different format with a comma (rather than line breaks and indentation) separating the values.
This a flow sequence style, and represents the same data as the regular sequence style.
citrus: [orange, lemon, lime]
stone: [peach, plum, cherry]citrus:
- orange
- lemon
- lime
stone:
- peach
- plum
- cherrySequences may be composed of scalars, other sequences, and maps. Maps can also be composed of sequences too.
This is where YAML gets complex. Pay extra attention to indentation.
Sequence of scalars and sequences and maps.
- 1
- B
- true
- 1
- 2
- 3
- MapKey1: value
- MapKey2: value
- MapKey3:
- Scalar value
- Scalar value
- Map:
- weather: sunny
temperature: 24
units: celsius
- weather: cloudy
temperature: 16
units: celsius
Does the pool sequence item map to swimming, outside, or inside?
outside:
- fun
- hiking
- swimming:
- river
- ocean
- lake
- pool
inside:
- readingSee answer
The pool item belongs to the outside mapping key.
The indentation level of pool is at the indentation of the sequence items belonging to the outside map.
