Reputation: 29
I am pretty new to tcl language. I am writing a script to automate a process. The code I wrote is below and the contents are read from a file called sample.tcl. The contents of this file changes often.. But I found the values stored in the variable old and new keeps on adding the changes instead of overwriting them. I would like to know what command must be used instead of lappend to do this.
Also if I do a echo $old there is something called tixScrolledListbox added at the beginning of the line. I am not sure what that mean. I would really appreciate if someone could guide me in this. TIA.
set fpp [open sample.tcl]
set b [read $fpp]
set c [split $b "\n"]
set item [llength $c]
set actual_column [expr $item -1]
set actual_item [lreplace $c $actual_column $actual_column]
close $fpp
foreach element $actual_item {
set f [regexp {(.*)\t+(.*)} $element var1 var2 var3]
set var_2 [regsub -all {\s} $var2 {}]
set var_3 [regsub -all {\s} $var3 {}]
lappend old $var_2
lappend new $var_3
}
Upvotes: 0
Views: 957
Reputation: 13282
There are a number of setting commands in Tcl, including lappend
and set
.
The lappend
command sets the value of a variable to be the list contained in the variable (possibly empty) with the assigned value added as a last element (it can also be several values added as a new tail to the old list). This is what you typically use if you want to store a sequence of values.
The set
command replaces the value in the variable with the assigned value (or returns the value in the variable, of no second argument is provided). This is what you use if you want to get rid of an old value before you store a new value.
You probably want to write something like this:
set old [list]
set new [list]
foreach element $actual_item {
# ...
lappend old $var_2
lappend new $var_3
}
This will get rid of any previous contents in old
and new
, and then store a value from each iteration in them.
Your code is a bit hard to figure out, but these lines:
set item [llength $c]
set actual_column [expr $item -1]
set actual_item [lreplace $c $actual_column $actual_column]
seem to mean that you want the variable actual_item
to hold all items in c
except for the last. If so, you can do that with this invocation instead:
set actual_item [lrange $c 0 end-1]
Note that you should always put the arguments to expr
in braces, like
set actual_column [expr {$item - 1}]
This form is safer and more efficient.
You also assign the return value of the regexp
call in f
but never use that variable.
Would something along the lines of
set fpp [open sample.tcl]
set b [read $fpp]
close $fpp
set old [list]
set new [list]
foreach {o n} [regexp -all -inline -- {[^\n\t]+} $b] {
lappend old [regsub -all {\s} $o {}]
lappend new [regsub -all {\s} $n {}]
}
be helpful? When I use that code on the text
a b 1 2
c d 3 4
(note: there is a tab between the "d" and the "3".)
I get old
= ab cd
and new
= 12 34
.
This snippet does not remove the last item in each line. If you still want to do that, it's easy to modify it so it does.
Documentation: close, expr, foreach, lappend, llength, lrange, lreplace, open, read, regexp, set
Upvotes: 1