Skip to content

Segment: Hash Attribute Searches

William W. Kimball, Jr., MBA, MSIS edited this page May 19, 2021 · 16 revisions

One of the more powerful YAML Path segment types, Hash attribute searches enable users to select from a Hash's immediate children only those which match some criteria. This includes searching attributes of an Array of Hashes. There are three forms of this search:

  • Search the value of a named attribute in each element of an array-of-hashes
  • Search the value of a named attribute
  • Search the Hash's key names

Along with the examples below, see Search Expressions for details of how to express Hash Attribute Searches.

Searching an Array of Hashes

warriors:
  - name: Chi-chi Shiranui
    power_level: 5280
    style: Dragon fury
  - name: Goku Higashi
    power_level: 9001
    style: Z fight
  - name: Krillin Bogard
    power_level: 5280
    style: Fatal ball
  - name: Bulma Sakazaki
    power_level: 1024
    style: Super final

When searching an array-of-hashes, indicate which attribute to search and the search condition within a required [] pair. Some examples using the YAML data above:

  • Users could search for any warriors with a power_level over 9,000: warriors[power_level > 9000]. This would return a single-result Hash:
{"name": "Goku Higashi", "power_level": 9001, "style": "Z fight"}
  • Searching for warriors with a power_level of exactly 5,280 using warriors[power_level = 5280] would return two Hash results:
{"name": "Chi-chi Shiranui", "power_level": 5280, "style": "Dragon fury"}
{"name": "Krillin Bogard", "power_level": 5280, "style": "Fatal ball"}

Searching the Value of a Named Attribute

car:
  make: Honda
  model: del Sol
  trim: VTEC
  color: Isle Green Pearl
  year: 1994

Perhaps more useful for validation purposes, YAML Path enables searching a named attribute of a Hash against a condition. Using the car data above, a user might wish to validate that the "object" result is expected by comparing one of its attributes against a known condition. For example:

  • Does the result contain a car made after 1970? car[year > 1970] returns 1994; getting the value at all indicates the result is valid.
  • Is the car a Ford? car[make = Ford] returns nothing; getting no result from the query indicates the object does not match the expectation.

Searching the Key Names of a Hash

contrast_ct:
  appendicitis: .009
  colitis: .002
  diverticulitis: .015
  gastroenteritis: .007
  ileus: .227
  large_bowel_obstruction: .095
  peptic_ulcer_disease: .007
  small_bowel_obstruction: .355
  ulcerative_colitis: .010

YAML Path also enables searching a Hash's keys rather than their values. Simply substitute a . for the attribute name in the search expression to indicate a key name search. Using the contrast_ct sample above, a user could query for all results containing the word bowel with contrast_ct[. % bowel]. This would return two results:

0.095
0.355

Bonus: How might you determine why the patient with those contrast CT results visited the hospital with abdominal discomfort? You could use a tool like yaml-paths, like so:

# Find results with a greater than 1 in 3 probability:
$ yaml-paths --search='>0.33' --values contrast_ct.yaml 
contrast_ct.small_bowel_obstruction: 0.355
Clone this wiki locally