2015-09-03

Hello, world!

The classic first demonstration program.

pack [button .b -text "Hello, world!" -command exit]

In a Tcl/Tk shell such as wish, this program creates a window and places a plain, unstyled button on it. The text on the button is "Hello, world!" and when you click the button, the program exits.

If you just want to print the text inside the shell, try

puts "Hello, world!"

2015-09-01

Writing sequentially

The challenge is as follows.

1) Pick out code5, the second part of the last word on the last line of this data file:

line1 1 2 3 4 5 end-code1-line
line2 2 2 3 4 5 end-code2-line
line3 3 2 3 4 5 end-code3-line
line4 4 2 3 4 5 end-code4-line
line5 5 2 3 4 5 end-code5-line

2) Do so by executing commands written sequentially ("pipelining" the data from one command to the next).

What we need to do is to read the data from the file, remove ("trim") whitespace from the beginning and end of it, split it into lines, pick the last line, split it into words, pick the last word, split it at the dashes, choose the second element of it.

The most natural way to solve this in Tcl is something like

package require fileutil

set data [string trim [::fileutil::cat lines.txt]]
set line [lindex [split $data \n] end]
set word [lindex [split $line] end]
lindex [split $word -] 1

This is not written in sequential order. The first command to be executed on each line is the one in the innermost brackets, then the command in the enclosing set of brackets, and so on.

The same code in sequential order looks like this:

package require fileutil

set v lines.txt
set v [::fileutil::cat $v]
set v [string trim $v]
set v [split $v \n]
set v [lindex $v end]
set v [split $v]
set v [lindex $v end]
set v [split $v -]
lindex $v 1

This works, but this is Tcl. We can abstract away a lot of detail here.

One idea is to enumerate the commands to be applied to the data and execute them in a loop:

proc seq {v cmds} {
    foreach cmd $cmds {
        set v [{*}$cmd $v]
    }
    set v
}

seq lines.txt {::fileutil::cat {string trim}}

But that only works as far as the string trim. The next command, to split the lines, requires a second argument.

We can solve that by creating command aliases:

interp alias {} lines {} apply {v {split $v \n}}
interp alias {} words {} split
interp alias {} last {} apply {v {lindex $v end}}

One of those, words, is unnecessary since that invocation doesn't need any further argument. It makes the code a bit clearer, though.

The other aliases work by rewriting invocations: the invocation last {a b c} is rewritten as lindex {a b c} end and so on.

Now we can perform the processing as far as seq lines.txt {::fileutil::cat {string trim} lines last words last}. The last two commands could have been aliased the same way, but let's be a bit more creative. If we define the convention that a command that contains the character / has an attached argument, we can let our code do the same rewriting as the aliases do.

proc seq {v cmds} {
    foreach cmd $cmds {
        if {[string match */* $cmd]} {
            set cmd [list apply [list v [regsub / $cmd { $v }]]]
        }
        set v [{*}$cmd $v]
    }
    set v
}

The if looks a little busy, but it's actually quite straightforward. The condition is that if $cmd contains a slash character somewhere (the asterisks state that there may be, but doesn't have to be, characters before and after the slash). Then a regsub command replaces that slash with the text { $v }, i.e. space, dollar, v, space. The resulting string is wrapped in a list with the text v as first element, and then that is wrapped in a list with apply as first element. The result becomes the rewritten command: if we had lindex/end, we now have {apply {v {lindex $v end}}}, which can be executed by set v [{*}$cmd $v].

And we can finally write the procedure sequentially. Compare with the second program above.

seq lines.txt {
    ::fileutil::cat
    {string trim}
    lines
    last
    words
    last
    split/-
    lindex/1
}