TCL Get the value of a second level variable - tcl

I à stuck with a TCL issue. I would like to have access to the content of a second level variable with keeping the format (list).
please see my code :
At first, I declare variable contents
set x1y {1 2 3 4}
set x2y {10 11 12 13}
After I perform a for loop
for { i 0} {i < 4} { incr i}
I would like to have in xy variable the content of x1y with keeping the list format
set xy [eval ["x${i}y"]]
foreach x $xy {
....
}
Do you have any idea / proposal. I tried subst but it doesn’t keep the format.
Thank you in advance

The way to read from a variable whose name is not a constant is to use the single argument form of set:
set x1y {1 2 3 4}
set x2y {10 11 12 13}
foreach i {1 2} {
foreach val [set x${i}y] {
puts "$i --> $val"
}
}
However, it is usually easier to make an alias to the variable with upvar 0, like this:
foreach i {1 2} {
upvar 0 x${i}y xy
foreach val $xy {
puts "$i --> $val"
}
}
And in almost every case where you're doing this, you should consider using arrays instead (remembering that Tcl's arrays are associative arrays; you can use compound keys as well as simple integers):
set xy(1) {1 2 3 4}
set xy(2) {10 11 12 13}
foreach i {1 2} {
foreach val $xy($i) {
puts "$i --> $val"
}
}
You probably want to try to avoid using eval or subst for this sort of thing; those commands have side effects that may hurt the stability of your code if you're not careful. Definitely not ones for cases like these. (Also, they'll be slower as they force Tcl to recompile its internal bytecode more frequently. All the solutions I present above don't have that misfeature.)

set x1y {1 2 3 4}
set x2y {10 11 12 13}
for {set i 1} {$i <= 2} {incr i} {
foreach e [set x${i}y] {
puts $e
}
}

Related

To add preceding digits in a list in Tcl

I am trying to incorporate the preceding digits in a numerical list, say 1-14, before the list 15-43.
I am using TCL for writing my code.
I am expecting the list should be 1,2,3,....43, instead the list is coming to be 15,16,17,...43.
I have tried to incorporate the missing numbers as follows:
set nres2 ""
set x 0
set y [lindex $nres $x]
while {$x < [llength $nres]} {
set i [lindex $nres $x]
while {$y < $i} {
lappend nres2 $y
incr y
}
incr x
incr y
}
This will incorporate missing numbers within a list like, if a list 15,16,17...,43 do not have numbers like 18, 22, 34, etc., it will incorporate these numbers in a separate list named as nres2.
But, I cannot include the preceding numbers of a corresponding list.
Any comments/suggestions will be greatly helpful.
Thanks in advance...
Prathit Chatterjee
I would change the y at the start:
set nres2 ""
set x 0
set y 1
while {$x < [llength $nres]} {
set i [lindex $nres $x]
while {$y < $i} {
lappend nres2 $y
incr y
}
incr x
incr y
}
With nres as 4 6 8 9, nres2 becomes 1 2 3 5 7, which is what I think you are trying to get.
Jerry posted already the immediate fix, but you may think about general improvements over your implementation using pairwise comparisons:
proc printMissings1 {lst} {
set b [lrepeat [lindex $lst end] 0]
foreach i $lst {
lset b $i 1
}
lsearch -exact -integer -all $b 0
}
printMissings1 $nres
lrepeat is used to create a Tcl list (array) of [llength $nres] elements.
lset is used to mark the elements whose indices correspond to values in the given range.
lsearch is used to return the indices of the elements left unmarked, which correspond to the missing values.

Sum of elements/numbers in N lists in Tcl?

How to add elements/numbers from n lists in tcl ?
I have tried in python and it worked using below:
[sum(x) for x in zip(*C)]
How to do this in tcl?
Is there a zip function in tcl?
Is there any other way to achieve this ?
I have 2 lists for now:
l1 {11 333 4 567 129}
l2 {23 47 56 10 13}
I can have N-number of lists
I need to return element wise sum from these lists.
for two lists I implemented below :
set result {}
foreach x $l1 y $l2 {
lappend result [expr {$x + $y}]
}
My concern is: I can have one or many lists. So in that scenario how do I implement?
To get a new list of the pairwise sums of the elements of two lists:
set result [lmap a $list1 b $list2 { expr {$a + $b} }]
Make the obvious addition for three lists, four lists, etc.
There are no function similar to zip in Tcl as far as I'm aware. You can however create one if you want to. A rough one might be like this:
proc zip {lists} {
set result {}
for {set i 0} {$i < [llength [lindex $lists 0]]} {incr i} {
set elem {}
foreach list $lists {
if {$i >= [llength $list]} {return $result}
lappend elem [lindex $list $i]
}
lappend result $elem
}
return $result
}
Then you can use it like this:
set l1 {11 333 4 567 129}
set l2 {23 47 56 10 13}
set ln [list $l1 $l2]
set sum [lmap x [zip $ln] {::tcl::mathop::+ {*}$x}]
# 34 380 60 577 142
Then you don't want a variable per list, you want something else like a list of lists.
This is my take on the above recommendation by Shawn:
set l1 {11 333 4 567 129}
set l2 {23 47 56 10 13}
lappend l $l1 $l2
proc zippedSum {p} {
set inner [llength [lindex $p 0]]
set result [list]
for {set i 0} {$i < $inner} {incr i} {
lappend result [::tcl::mathop::+ {*}[lmap sublist $p {lindex $sublist $i}]]
}
return $result
}
zippedSum $l
You would not maintain separate variables, but a list of lists (l). Then you would process the sublists per index (with the maximum index determined by the first sublist), using the lmap/lindex combo recommended elsewhere for what you call zipping.

expected integer but got "floating point number" error

I try to write a very simple program in TCL using list.
Below is the list
list { 1 2 3 4 5 6 1.5 7 }
Below is my code
set sum 0
for {set i 0} {$i < [llength $list]} {incr i} {
incr sum [lindex $list $i]
}
puts $sum
On executing the above program I am getting the below error due to floating point value of 1.5 in the list
expected integer but got "1.5"
(reading increment)
invoked from within
"incr sum [lindex $list $i]"
I searched on internet and could not find anything relevant.
Please advise how do I handle the floating point value?
While using incr command, variable must have value that can be interpreted as a an integer. See tcl wiki.
If variable is a non-integral real number, [incr] could not be used, but [set] could:
set sum 0
for {set i 0} {$i < [llength $list]} {incr i} {
set sum [expr {$sum + [lindex $list $i]}]
}
puts $sum
Omsai's answer should solve your problem, but a cleaner solution is to use foreach:
set sum 0
foreach n $list {
set sum [expr {$sum + $n}]
}
puts $sum
Summing up a list of numeric values can also be done with the ::tcl::mathop::+ command:
::tcl::mathop::+ {*}$list
This looks more complicated that it is. The + command isn't available in the regular namespace, so you need to specify where it comes from (the ::tcl::mathop namespace). The command expects to get each operand as a separate argument, so if they are in a list you need to expand that list using the {*} prefix.
foreach and the various mathop commands are documented here: foreach, mathop.
(Note: the 'Hoodiecrow' mentioned in the comments is me, I used that nick earlier.)
Tcl gives an error if you will try
incr a 1.5
you have to change the logic.
clearly you want to add all the numbers in the list. and answers are easy and many. But i will give you the shortest way:
set l { 1 2 3 4 5 6 1.5 7 }
set sum [expr [join $l +]]
NO LOOPING REQUIRED.

Combinations of all charcaters and all lengths with using less number of loops?

Brain Teaser: I self originated this question, but stuck completely.
I want to create all possible combination of all characters, but of all possible lengths. Suppose, [a-z] combination of 1 length, then [a-z] combination of 2 length, and so on till the maximum length achieved.
this could be very easily done by iterative looping.
Example for 3 length:
proc triples list {
foreach i $list {
foreach j $list {
foreach k $list {
puts [list $i $j $k]
}
}
}
}
But, it should solve using less loops (looping needs to be dynamic)
set chars "abcdefghijklmnopqrstuvwxyz"
set chars [split $chars ""]
set complete_length [llength $chars]
set start 0
set maximum_length 15
while {1} {
if {$start > $maximum_length} {
break
}
for {set i [expr $maximum_length-$start]} {$i >= 0} {incr i -1} {
# dump combinations
}
incr start
}
In this chunk, what algorithm or method i should apply? Any kind of suggestions/help/code will be appreciated.
Sry, this is not an answer, but hopefully some interesting discussion anyway:
The word "combinations" is often used way too generally, so it can be interpreted in many different ways. Let's say that you have a source list of 26 different elements, the english letters, and you want to pick 3 of them and combine in a 3 element destination list:
Can you always pick any letter from the source list, or do the elements disappear from it as you pick them? Either define "pick" (are the elements copied or moved during a pick), or define the set of source values (is there 1 of each of A-Z or an infinite amount of A-Z).
Does the order in the destination list matter? Is AHM considered to be the same combination as HAM? Define "combine".
If you have a list where not all elements are different, e.g. {2 10 10 64 100}, you have even more possibilities. Define your set of values.
Your first example prints permutations, not combinations. If that's what you want, the easiset way is a recursive procedure. Combinations are more complicated to generate.
EDIT:
I wrote this procedure for a Project Euler program. It picks all the elements, but maybe you can modify it to pick n. It takes a command prefix as argument, so you don't have to store all permutations.
package require Tcl 8.5.0
proc forEachPerm {list cmdPrefix} {
_forEachPerm {} $list $cmdPrefix
}
proc _forEachPerm {head list cmdPrefix} {
if {![llength $list]} {
{*}$cmdPrefix $head
} else {
for {set i 0} {$i < [llength $list]} {incr i} {
_forEachPerm [concat $head [lrange $list $i $i]] [lreplace $list $i $i] $cmdPrefix
}
}
}
# example use:
forEachPerm {a b c} {apply {{list} {puts [join $list]}}}

Is there shorthand in Tcl to get a sequential array of numbers?

For example, in Perl, to get a sequential array of numbers from 1 to 10, you could simply do:
#myArray = (1 .. 10);
The two periods serve as shorthand for this operations instead of making a for loop or writing the whole thing out manually. Other languages I've used have something similar also.
Does a similar shorthand exist in Tcl?
You can define the method:
proc fillArray {a b} {
eval return \[list $a [string repeat "\[incr a\] " [incr b -$a]]\]
}
And use it as:
set myArray [fillArray 1 10]
You even can beautify the call of procedure to make it look as in perl. For that just redefine unknown procedure:
rename unknown __unknown
proc unknown {args} {
if {[llength $args] == 3} {
lassign $args a op b
if {[string is integer $a] && $op == ".." && [string is integer $b]} {
return [fillArray $a $b]
}
}
return [uplevel __unknown {*}$args]
}
After that you can write just simple as:
set myArray [1 .. 10]
:)
Not quite this one, but
% package require struct::list
1.6.1
% struct::list iota 10
0 1 2 3 4 5 6 7 8 9
Also search this for the "iota" keyword to see how this can be done using a one-liner.
With the exception of expressions (which are their own little language) Tcl has no operators and is always a strictly prefix-driven language. This means that there isn't such a convenient shorthand for doing loops. On the other hand, there's nothing particularly special about Tcl's standard commands (apart from some minor efficiency details that don't matter here) so making your own is no problem:
proc .. {from to} {
if {$from >= $to} {
for {set i $from} {$i <= $to} {incr i} {lappend out $i}
} else {
for {set i $from} {$i >= $to} {incr i -1} {lappend out $i}
}
return $out
}
puts [.. 1 10]; # --> “1 2 3 4 5 6 7 8 9 10”
You can fake infix operators by using an unknown handler (as in GrAnd's answer) but that's really quite slow by comparison with the above.
No, a similar shorthand does not exist in tcl.
If you really want shorthand, you can create your own command that looks almost the same. For example:
proc : {start ignore end} {
set result []
for {set i $start} {$i <= $end} {incr i} {
lappend result $i
}
return $result
}
puts "from 1 to 10: [: 1 .. 10]"