bytes1234
bytes1234

Reputation: 153

TCL write data into a YAML file

I have a tcl script that is capturing some data. I would like to write this data to a YAML file. is there any examples or places I can read about how to do this in TCL ?

Upvotes: 3

Views: 3382

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137717

The yaml package in Tcllib does it.

If your data is a simple single-level dictionary, you can just do:

package require yaml

set yamlText [yaml::dict2yaml $yourDictionary]

For example:

% set d {foo 123 bar "this is a piece of text"}
foo 123 bar "this is a piece of text"
% yaml::dict2yaml $d
---
foo: 123
bar: this is a piece of text

% 

The major issue with this is that it assumes that everything in each key is really a string (though numbers are usually short enough that that comes out OK too).

Therefore, if your data is more complex, you need to construct a huddle description of it using the huddle package first. That embeds the extra type information required to generate complex structures.

package require huddle
package require yaml

set inner [huddle create boo "this is some text" grill "this is some other text"]
set outer [huddle create foo 123 bar $inner]
yaml::huddle2yaml $outer

Upvotes: 5

andy mango
andy mango

Reputation: 1551

The standard Tcl library (tcllib) has a yaml package in it. If that package does not do what you need, you can view the source for ideas.

Upvotes: 0

Related Questions