Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL write data into a YAML file

Tags:

tcl

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 ?

like image 426
bytes1234 Avatar asked Dec 15 '25 15:12

bytes1234


1 Answers

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
like image 77
Donal Fellows Avatar answered Dec 18 '25 07:12

Donal Fellows



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!