Reputation: 2868
For other language, self increment of list element is is very easy, just like:
list1(i) = list1(i) + 1
but for tcl, currently I use:
set temp [lindex $list1 $i];
lset list1 $i [expr $temp + 1];
Are there some other ways to improve it/make it simpler?
Upvotes: 2
Views: 3872
Reputation: 1
One-liner:
CODE:
set list1 [lreplace $list1 $index1 $index1 [expr {1+[lindex $list1 $index1]}]]
Best practice is to use arrays in such situations, as previously mentioned.
Upvotes: 0
Reputation: 16428
With Tcl list, I am not sure about any internal commands to increase the value of list element directly.
But, you can do it if you are using dictionaries in Tcl by means of dict incr
command.
dict incr dictionaryVariable key ?increment?
This adds the given increment value (an integer that defaults to 1 if not specified) to the value that the given key maps to in the dictionary value contained in the given variable, writing the resulting dictionary value back to that variable. Non-existent keys are treated as if they map to 0. It is an error to increment a value for an existing key if that value is not an integer. The updated dictionary value is returned.
set sample { firstName Dinesh lastName S title Mr age 23}
# This will increase the value of 'age' by 1
puts [ dict incr sample age 2]
# If any key used which does not exits in the dict, then it will create that key
# assume it's initial value is 0.
puts [ dict incr sample dob ]
Output :
firstName Dinesh lastName S title Mr age 25
firstName Dinesh lastName S title Mr age 25 dob 1
If you still interested in using increment in list element, then we can write up a new command for the same like as follows,
proc lincr { my_list index { increment 1 } } {
upvar $my_list user_list
if { [llength $user_list] <= $index } {
return -code error "Wrong list index"
}
lset user_list $index [ expr {[lindex $user_list $index] + $increment} ]
}
set val { 1 2 3 4 }
lincr val 1 4; # Increasing first index by 4
lincr val 0; #Increasing zeroth index by 1
puts $val
Upvotes: 5
Reputation: 13252
Well, you can use an array, which can be viewed as a sparse list. In that case the Tcl expression becomes very simple indeed:
incr list1($i)
You seem to somehow think that Tcl expressions are more "difficult" just because the syntax is different from what many other languages use. Your example
list1(i) = list1(i) + 1
means approximately "assign to (the member of list1 indicated by the value of i) the value of (the member of list1 dereferenced by the value of i) plus 1", and the following Tcl expression means exactly the same:
lset list1 $i [expr {[lindex $list1 $i] + 1}]
It looks different, it's almost double the length, and there are two kinds of brackets instead of just one, but it's still the exact same expression.
So is this, by the way (C language family):
list1[i] += 1;
/* or */
++list1[i];
or this (Lisp family):
(setf (elt list1 i) (+ (elt list1 i) 1))
; or
(incf (elt list1 i))
All these expressions basically mean the same thing and cause basically the same instructions to be executed by the processor. It's just a matter of preference (or managerial decision) which one you use.
For me, Tcl is a lot easier on the whole, because it gives me liberties and power that few other languages can give me.
Upvotes: 1