Reputation: 29
I have a two problems with my script in TCL language. First: I created list which download values from chart, but i dont know how to move elements in my list, for example:
set list [2,3,4,5,6] # my list
and i want this effect ->
[1,2,3,4,5] next -> [0,1,2,3,4] etc.
Second: How to create fixed size list for 10 elements? I want 10 elements in my list and when i got > 10 elements i want to remove last element and add new as first element (first problem)
Thanks and sorry for my bad english :)
Upvotes: 0
Views: 928
Reputation: 13252
To add a new item in the first position in the list, and at the same time limit the list to a given size:
set size 5
set list [list 2 3 4 5 6]
# => 2 3 4 5 6
set list [lrange [linsert $list 0 1] 0 $size-1]
# => 1 2 3 4 5
set list [lrange [linsert $list 0 0] 0 $size-1]
# => 0 1 2 3 4
You can simplify this a bit using a procedure:
proc move {varName new} {
upvar 1 $varName list
set size 5
set list [lrange [linsert $list 0 $new] 0 $size-1]
}
set list [list 2 3 4 5 6]
# => 2 3 4 5 6
move list 1
# => 1 2 3 4 5
move list 0
# => 0 1 2 3 4
Documentation: linsert, list, lrange, proc, set, upvar
Syntax of Tcl index expressions:
end
the last elementend
-N the nth element before the last elementend
+N the nth element after the last element (in practice, N should be negative)There can be no whitespace within the expression.
Upvotes: 2