4

Is there a way to reference a particular element in a yaml array? For example if I have this bit of yaml:

node_list:
  - one
  - two
  - three

Can I do something like this:

first_node: node_list[0]
dreftymac
  • 31,404
  • 26
  • 119
  • 182
Red Cricket
  • 9,762
  • 21
  • 81
  • 166
  • YAML has no array construct. What you are looking at is called a sequence and that can be represented on loading in your programming language to something called differently. E.g. if you work with Python sequences are represented as `list`. If you talk about YAML generically and not in relation to some specific programming language, please use the appropriate terminology from the specification. – Anthon May 17 '17 at 10:14
  • There are myriad flavors of data structures, but they can all be adequately represented with three basic primitives: mappings (hashes/dictionaries), sequences (arrays/lists) and scalars (strings/numbers). - http://yaml.org/spec/1.2/spec.html#Introduction – Red Cricket May 17 '17 at 18:19
  • @RedCricket: The paragraph you refer to is read as: name of the primitive is **sequence**, which can represent array or list. Similarly: name of the (another) primitive is *scalar*, which can represent string or number. While in given case it is obvious that you talk about "sequences", notion of "array" in in other YAML context could be misleading. – Tsyvarev May 19 '17 at 23:19
  • Oh I thought it read as "There are myriad flavors of data structures, but they can all be adequately represented with three basic primitives: mappings (hashes/dictionaries), sequences (arrays/lists) and scalars (strings/numbers)". Maybe I need glasses. – Red Cricket May 20 '17 at 00:47

1 Answers1

7

Only with anchors & aliases. Example:

node_list:
  - &first one
  - two
  - three
first_node: *first

There is no equivalent to XPath in YAML, at least not officially.

flyx
  • 35,506
  • 7
  • 89
  • 126