Ariel Baron Ariel Baron - 1 year ago 110
C++ Question

How to emit a list separated by commas in YAML

I have a 9 3x3 matrix of all zeroes. I am trying to output it in row major form so that it will look like.
covariance: 0,0,0,0,0,0,0,0,0,

I have tried

out1 << YAML::Key << "covariance";
out1 << YAML::Literal<< covariance[0][0]<< "," << covariance[0][1]... etc.

But i get :

covariance: 0
",": 0
what am I doing wrong?

note: I DO NOT want [0,0,0,0,0,0,0,0,0]. I would like it without the brackets

Answer Source

Build a string up, and then print it out:

std::stringstream value;
for (int i=0;i<N;i++) {
  value << covariance[0][i];
  if (i + 1 < N) {
    value << ",";
YAML::Emitter out;
out << YAML::BeginMap;
out << YAML::Key << "covariance" << YAML::Value << value.str();
out << YAML::EndMap;

The point is that the value you're printing isn't a YAML list, it's just a plain string that happens to look a little bit like YAML. So you can't use yaml-cpp to format it for you; you need to do that yourself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download