Skip to content

Segment: Hash Slices

William W. Kimball, Jr., MBA, MSIS edited this page Mar 1, 2020 · 5 revisions

Somewhat similar to Array Slices and using all rules for Hash Keys, a Hash Slice is a resultant Hash that is composed of only the values from a source Hash for which their keys have names fitting between an alphanumeric start and end value, inclusively. Hash Slice searches are performed against the names of the keys but only the values of matching keys are returned. The locale of the run-time environment controls sorting and matching behavior. The result is always the actual matched values in their original data types, one result per match. So, some results may be scalar values, some may be Arrays, and some may be Hashes; it all depends on what the source value are. Like Array Slice segment notation, a [] pair and the : symbol are required.

Take for example, this YAML data:

hash_name:
  a_key: value_a
  b_key: value_b
  c_key: value_c
  d_key: value_d
  e_key: value_e

In order to inclusively select the values of keys from b_key through d_key, you could use these YAML Paths: hash_name[b_key:d_key] or /hash_name[b_key:d_key].

Clone this wiki locally