I have data that is {} {abc}, {abc} {} ,or {abc} {def}, and I want to capture it in 2 variables. I tried:
foreach {fname} <program to get values> {
set dfrom [lindex $fname 1]
set rname [lindex $fname 2]
print "fname- $fname"
print "dfrom- $dfrom"
print "rname- $rname"
}
However, {} is not getting an index.
From the manual about lindex, emphasis mine:
When presented with a single index, the lindex command treats list as a Tcl list and returns the index'th element from it (0 refers to the first element of the list)
So you will have to use something more like this:
foreach {fname} <program to get values> {
set dfrom [lindex $fname 0]
set rname [lindex $fname 1]
print "fname- $fname"
print "dfrom- $dfrom"
print "rname- $rname"
}
And if you're on Tcl 8.5 or newer versions, you can use lassign:
foreach {fname} <program to get values> {
lassign $fname dfrom rname
print "fname- $fname"
print "dfrom- $dfrom"
print "rname- $rname"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With