From: Youness A. <kak...@ka...> - 2004-10-26 19:12:59
|
message still not received, I'm sending it again.. btw, the "string isdigit" should be replaced by "string is digit" in the code below... KaKaRoTo my proc lset { } was just an example.. you must read the tcl man page of the command lset... you'll see that it can take as many indices as it wans, so it can set an element in a sublist of the list... we should make the new lset proc use infinite arguments, and it must be a recursive function... or change the lines in the plugins.tcl file to this : set sublist [lindex ::plugins::found $idx] lset $sublist 5 [file join [file dirname $file] [lindex [lindex $::plugins::found $idx] 5]] lset ::plugins::found $idx $sublist and btw, the lset proc I made returns the list, and it takes a list, not the variable name... so we should do instead : proc { varname idx newvalue } { upvar 1 $varname list set $list [ .... ] return $list } let me see if I can get the lset proc correctly : proc { varname args } { upvar 1 $varname list # Check args if { [llength $args] < 1 } { error "Wrong # of args : should be \"lset varname ?indices? newvalue\"" } # set our necessary variables, the indices we got, the first index we got, the new indices to sent recursively and the new value to assign to the list set indices [lrange $args 0 end-1] set idx [lindex $indices 0] set newindices [lrange $indices 1 end] set newvalue [lindex $args end] # verify the index is a digit, or is "end" or "end-digit" if { [string is didit $idx] == 0 && [string first "end" $idx] != 0 } { error "Invalid index" } # if the index begins with "end" if { [string first "end" $idx] == 0 } { # if the index is index, set the number of chars before the end to 0 if { $idx == "end" } { set minus 0 } else { # remove the "end" word from the index set firstidx [string first "end" $idx] set end [string range $idx [expr $firstidx + 3] end] # if the index isn't "end-" then raise error if { [string fist "-" $end] != 0 } { error "Invalid index" } else { # Remove the minux sign and verify the rest is digit set end [string range $idx 1 end] if { [string is didit $end] == 0} { error "Invalid index" } else { # if the index was "end-digit", set that digit to be the number of chars before the end to be taken set minus $end } } } # set the new index accordingly (from end to last index of list, from end-5 to last index of list - 5 , etc...) set idx [expr [llength $list] - $minus -1] } # if there is no index, then set the value if { [llength $indices] == 0 } { set $list [list $newvalue] } else { # if the index is out of bounds, raise an error if { $idx < 0 || $idx > [expr [llength $list] -1]} { error "Index out of bound" } # retreive our sublist, then change it recursivly with the indices minux the first index set sublist [lindex $list $idx] lset sublist $newindices $newvalue # set the new list accordingly if { $idx == 0 } { set list "$sublist [lrange $list [expr $idx + 1] end]" } else if { $idx == [expr [llength $list -1] ] } { set list "[lrange $list 0 [expr $idx -1]] $sublist" } else { set list "[lrange $list 0 [expr $idx -1]] $sublist [lrange $list [expr $idx + 1] end]" } return $list } } ok, good, it should work, even with hundreds of indices, and with an index of "end" or "end-5" or whatever... I hope it works, I wrote it on my mail client, so didn't test it or anything.. tell me if it works, and also if it doesn't.. try to find the problem... KaKaRoTo On Tue, 26 Oct 2004 10:57:42 -0200 (BRST), Mauricio Teixeira (netmask) <ne...@we...> wrote: > > Youness Alaoui disse: > >> yep, 2 problems, first, [lindex $list $idx $idx2] doesn't work in tcl >> 8.3, >> the solution would be [lindex [lindex $list $idx] $idx2], then the lset > > Correct. > >> command doesn't exist at all, so we should write our own lset proc : >> proc lset { list index newvalue } { > > Error in startup script: called "lset" with too many arguments > while executing > "lset ::plugins::found $idx 5 [file join [file dirname $file] [lindex > [lindex $::plugins::found $idx] 5]]" > -- KaKaRoTo |