在 TCL 中比较两个列表的正确方法是什么?

发布于 2024-10-20 11:22:22 字数 246 浏览 1 评论 0原文

我是 TCL 的新手,我编写了以下代码:

set list1 {{1 2} 3 4}
set list2 {{1 2} 8 1}
if {[lindex $list1 0] == [lindex $list2 0]} { puts "They are equal!"}

但是当我打印子列表元素时,我看到它们是相等的,但是 if 语句没有捕获它。为什么?我应该如何纠正这种比较?

I am newbie to TCL and I have written the following code:

set list1 {{1 2} 3 4}
set list2 {{1 2} 8 1}
if {[lindex $list1 0] == [lindex $list2 0]} { puts "They are equal!"}

But when I print the sublist elements I see that they are equal, but the if statement does not catch it. Why? How I should right this comparision?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁吢 2024-10-27 11:22:22

我会这样做:

# from tcllib
package require struct::list


if {[::struct::list equal $list1 $list2]} { puts "Lists are equal"}

I would do:

# from tcllib
package require struct::list


if {[::struct::list equal $list1 $list2]} { puts "Lists are equal"}
稳稳的幸福 2024-10-27 11:22:22

如果我要实现一个 lequal 过程,我会从这个开始:

proc lequal {l1 l2} {
    foreach elem $l1 {
        if {$elem ni $l2} {return false}
    }
    foreach elem $l2 {
        if {$elem ni $l1} {return false}
    }
    return true
}

然后优化到这个:

proc K {a b} {return $a}

proc lequal {l1 l2} {
    if {[llength $l1] != [llength $l2]} {
        return false
    }

    set l2 [lsort $l2]

    foreach elem $l1 {
        set idx [lsearch -exact -sorted $l2 $elem]
        if {$idx == -1} {
            return false
        } else {
            set l2 [lreplace [K $l2 [unset l2]] $idx $idx]
        }
    }

    return [expr {[llength $l2] == 0}]
}

If I were to implement an lequal proc, I'd start with this:

proc lequal {l1 l2} {
    foreach elem $l1 {
        if {$elem ni $l2} {return false}
    }
    foreach elem $l2 {
        if {$elem ni $l1} {return false}
    }
    return true
}

And then optimize to this:

proc K {a b} {return $a}

proc lequal {l1 l2} {
    if {[llength $l1] != [llength $l2]} {
        return false
    }

    set l2 [lsort $l2]

    foreach elem $l1 {
        set idx [lsearch -exact -sorted $l2 $elem]
        if {$idx == -1} {
            return false
        } else {
            set l2 [lreplace [K $l2 [unset l2]] $idx $idx]
        }
    }

    return [expr {[llength $l2] == 0}]
}
剪不断理还乱 2024-10-27 11:22:22

它们并不相等,并且您对此进行了正确的测试。确定你打印了正确的变量吗?

编辑:对我来说的行为。

# cat test.tcl
set list1 {{1 2} 3 4}
set list2 {{1 2} 8 1}
if {[lindex $list1 0] == [lindex $list2 0]} { puts "They are equal!"}
# tclsh test.tcl
They are equal!
#

They're not equal, and you test correctly for that. Sure you're printing the right variables?

EDIT: Behavior for me.

# cat test.tcl
set list1 {{1 2} 3 4}
set list2 {{1 2} 8 1}
if {[lindex $list1 0] == [lindex $list2 0]} { puts "They are equal!"}
# tclsh test.tcl
They are equal!
#
那些过往 2024-10-27 11:22:22
#compare two lists with hash table
if {[llength $list1] ne [llength $list2]} {
    puts "number of list elements is different"
} else {
    puts "there are [llength $list1] list elements"
}
set i 1
foreach a {$list1} b {$list2} {
    set a($i) $a
    set b($i) $b
    incr i
}
for {set j 1} {$j <= [llength $list1]} {incr j} {
    if { $a($j) ne $b($j) } {
        puts "No Match $a($j) $b($j)"
    } else {
      puts "Match $a($j) $b($j)"
    }
}
#compare two lists with hash table
if {[llength $list1] ne [llength $list2]} {
    puts "number of list elements is different"
} else {
    puts "there are [llength $list1] list elements"
}
set i 1
foreach a {$list1} b {$list2} {
    set a($i) $a
    set b($i) $b
    incr i
}
for {set j 1} {$j <= [llength $list1]} {incr j} {
    if { $a($j) ne $b($j) } {
        puts "No Match $a($j) $b($j)"
    } else {
      puts "Match $a($j) $b($j)"
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文