1
votes

What's the best way to save a dictionary to a file, so that I can load it later in Tcl on a different computer/system?

I don't think fconfigure $stream -translation binary; puts -nonewline $stream $dict works if there's keys/values with unicode characters > \u00ff. Is "utf-8" encoding OK (to save disk space), or should I always use full "unicode"?

Dictionaries are new to me, but since their textual representation is that of a list with alternating keys/values, and a list is just a string with some extra syntax characters, maybe the question could've been "Safely save a string to file"?

1

1 Answers

2
votes

Your question can be simplified:

What encoding should I use?

The answer is: it depends.

  • If you have only binary data, then binary is the way to go.
  • If you have mostly ascii chars and a few exceptions (like umlauts etc), then utf-8 ist the best encoding for you (characters outside of \x00 and \x7f are encoded with 2 or more bytes, ascii with 1 byte).
  • If you have many CJK, then using unicode or some other encoding for that is probably better (2 byte vs 3 byte per char)

And right: the (de)serialization of lists and dicts is in Tcl much easier than in many other languages: Save it as string.