Your foreach loop is not being fed a proper Tcl List. It’s just that big file of segments. The loop is going to split it on any whitespace it finds. You need to split it into a correctly-formed list of messages prior to the foreach loop. Think about splitting on the n characters that you already inserted.