How to read input files: YAML

YAML stands for “Yet Another Markup Language (YAML) Ain’t Markup Language (YAML^2)”. Somewhat confusing it may seem. The aim of the project which started in early 2000s was first to create yet another markup language with a different syntax. The purpose later changed from just a pure markup language to data oriented and hence the YAML^2. One of the main focusses is user readability.

YAML’s approach is similar to XML and JSON, where parsing data in files into structures of the most common programming language is targeted. Actually, YAML is a superset of JSON, i.e. it allows JSON syntax. The language allows encoding of scalars (i.e. integers, strings etc), lists and associated arrays (cf. python’s dictionaries or hashes). What follows is the syntax of how scalars, list and associated arrays (in that specific order) are created in a .yaml file:

nbr_students: 2 

  - "A"
  - "B"

  Anne: "A"
  Danny: "A"

Note how easy it is to read and understand the content. And now observe how easy it is to write a c++ script (with yaml-cpp) and obtain the grade of student Anne:

#include "yaml-cpp/yaml.h"

YAML::Node file = YAML::LoadFile("test.yaml");
std::string annes_grade = file["student_grades"]["Anne"].as<std::string>(); 

From now on this is how I provide manual inputs to my analyses.