0

I am currently writing a Tcl script for NS2 where things are added to a list via the lset x 0 end+1 $item command in Tcl. See below the below test script:

set x { {} {} {} }
set refID 1
proc addValue {value} {
    global x refID
    set value [expr $value*2]
    lset x $refID end+1 $value 
} 
addValue 7
addValue 8
# => {} {14 16} {}

Works perfect! The problem is that this is written using Tcl 8.6 by running the command tclsh test.tcl. The actual code I am trying to run is run using NS2 with Tcl version 8.5. Tcl 8.5 does not support the lset end function. How can I update Ns2 to run on Tcl 8.6? I have 8.6 on my computer; I just do not know how to get Ns2 to use it instead of 8.5. Using the lset in this way would save me quite a bit of time and a bunch of code.

Thank you

Sam Dean
  • 103
  • 2

1 Answers1

0

Here's a workaround: reimplement the "end+1" functionality with plain tcl 8.5

rename lset _tcl_lset

proc lpop {varname} {
    upvar 1 $varname var
    set value [lindex $var end]
    set var [lrange $var 0 end-1]
    return $value
}

proc lset {varname args} {
    upvar 1 $varname var
    set value [lpop args]
    set indices $args

    if {[lindex $indices end] eq "end+1"} {
        lpop indices
        set sublist [lindex $var {*}$indices]
        lappend sublist $value
        set args [list {*}$indices $sublist]
    }

    _tcl_lset var {*}$args
}
glenn jackman
  • 25,463
  • 6
  • 46
  • 69