Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with pattern of list with values surrounded by {}

Tags:

tcl

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.

like image 775
user3277557 Avatar asked Dec 29 '25 20:12

user3277557


1 Answers

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"
}
like image 190
Jerry Avatar answered Jan 01 '26 19:01

Jerry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!