Gerald W. Lester <Gerald.Lester@cox.net> wrote:
> If it does not need to be pretty (i.e. you want to save it to read in again
> later):
> puts $outChan [list array set arrayName [array get arrayName]]
sure, for reading it in later, this is *the* way to do it.
In my case it was for a human reader to check if the array contained
the right things. The only requirement was, that the code should fit
in one or two lines and only at those places where something was
watched, which excluded writing up my own procedure.
Instead, I wrote:
set xx ""; foreach {k v} [array get myarr] {append xx "x($k)=$v\n"}
exec echo $xx > /tmp/avl-logfile
Even shorter would have been:
exec echo "myarr:\n[join [array get myarr] "\n"]\n" > /tmp/avl-logfile
But I could afford two lines, and I found it worth the luxury :-)
(why "shelling"(I know, no /bin/sh involved!) out to echo?
Because that takes fewer chars than open/puts/close would. :-)
Flattened lists (like what is returned by array get, or listified dicts)
are a bit tough to handle in Tcl. The only command that really handles
them sensibly is foreach with multiple variables.
I propose some extension for [join] (and, unless the echo here
is mostly negative or nonexistent, I'll likely tip it up later):
example first:
set list {1a 1b 1c 2a 2b 2c 3a 3b 3c}
puts >[join $list . , \n]<
>1a.1b,1c
2a.2b,2c
3a.3b,3c<
join _now_ accepts the list and _one_ (optional) string.
join would *then* accept the list and an *arbitrary number* of
strings which would be cycled through.
Received on Thu Sep 29 14:20:11 2005