Using lists in tcl

Lists

Some functions will return lists. I have no idea what form is comfortable in your favorite scripting language, and I'm sure there is no one single format that would make everyone happy. I picked a format that I liked the best. Works very fine with tcl's lists and foreach feature and wouldn't be hard to use for example from awk or php. I think this format is still a nightmare to use from c.

Each element of the list may contain an identifier and a value separated by a single =. Elements are separated by | characters to allow having spaces in both identifiers and values. We don't have escaping. Yes, this means you can't pass "=" or "|" in a value. Example:

		south=45 north=11
	

From tcl, you can cycle trough the list with a script like this, assuming you have your list in a string called $string:

		foreach {name value} [split $string "|="] {
			puts "$name ==> $value"
		}
	
NOTE: it's very important that you do NOT put {} around split, since that would make the splitted string assembled into one long word again.

Coordinates

We represent coordinates in X:Y form, with decimal numbers. For example 164:22 is a valid coordinate. You can split it into a single x and y variables this way:
		foreach {x y} [split $string ":"] {}
	

Coordinates on a list

Both identifier and value may be a coordinate as well as anything else. However, it's particularly useful to use coordinates as identifiers in cases when a c function searches an area or path. In this section, an example is given how to process the result in tcl.

Let's say the result is in form "X:Y=D". In this case, D is a direction. An example for the list:

		"144:23=NE 142:23=SW"
	

You can process the above string with a script like this:

	foreach {x y d} [split $string "|:="] {
		puts "$x : $y -> $d"
	}