2

Taking this yaml node for example:

- flow:
    - do:
        ? command:
            - command1: command
            - command2: command
            - command3: command
          name: nameblock
          descr: descrblock
        : block_1

For the value "block_1", the key is a map node. How can I use yaml-cpp to delete the most inner value "block_1", so that the whole node becomes:

- flow:
    - do:
          command:
            - command1: command
            - command2: command
            - command3: command
          name: nameblock
          descr: descrblock

Any suggestion? Appreciated!

1 Answer 1

1

You basically have to reassign the whole containing node, rather than delete anything. For example:

YAML::Node root = YAML::LoadFile("test.yaml");

// this is now the value of the "do" node
// explanation of each value:
// root[0]  - zeroth entry in the top-level sequence
// ["flow"] - value for the key "flow"
// [0]      - zeroth entry in the resulting sequence
// ["do"]   - value for the key "do"
YAML::Node node = root[0]["flow"][0]["do"];

// we're assuming there's only one entry in the map
// if you want a particular one, you can hunt for it
assert(node.size() == 1);  

// this is the key of the first key/value pair
YAML::Node key = node.begin()->first;

// update the whole key/value pair to be just the key
node = key;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.