Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何按值对Tcl数组进行排序?_Arrays_Sorting_Tcl - Fatal编程技术网

Arrays 如何按值对Tcl数组进行排序?

Arrays 如何按值对Tcl数组进行排序?,arrays,sorting,tcl,Arrays,Sorting,Tcl,例如,如何对数组输出进行排序 来自 放置“$word$count($word)”} 样本输入 Roger 15 Martin 18 Jemmy 16 Jon 12 Sara 12 Martin 18 Jemmy 16 Roger 15 Jon 12 Sara 12 预期产出 Roger 15 Martin 18 Jemmy 16 Jon 12 Sara 12 Martin 18 Jemmy 16 Roger 15 Jon 12 Sara 12 你可能有类似的东西 array set co

例如,如何对数组输出进行排序 来自
放置“$word$count($word)”}

样本输入

Roger 15
Martin 18
Jemmy 16
Jon 12
Sara 12
Martin 18
Jemmy 16
Roger 15
Jon 12
Sara 12
预期产出

Roger 15
Martin 18
Jemmy 16
Jon 12
Sara 12
Martin 18
Jemmy 16
Roger 15
Jon 12
Sara 12

你可能有类似的东西

array set count { Roger 15 Martin 18 Jemmy 16 Jon 12 Sara 12 }
foreach word [array names count] {puts "$word $count($word)"}
您要做的是将数组转换为一个列表,成对地跨过它并根据数字对其排序:

foreach {name num} \
        [lsort -integer -decreasing -stride 2 -index 1 [array get count]] \
        {puts "$name $num"}
参考文献:


你可能有类似的东西

array set count { Roger 15 Martin 18 Jemmy 16 Jon 12 Sara 12 }
foreach word [array names count] {puts "$word $count($word)"}
您要做的是将数组转换为一个列表,成对地跨过它并根据数字对其排序:

foreach {name num} \
        [lsort -integer -decreasing -stride 2 -index 1 [array get count]] \
        {puts "$name $num"}
参考文献:

Tcl的数组总是未排序的,事实上,在添加元素时(当重建基础哈希表时),元素的顺序会不时发生变化。要获得所需的输出,最好获取数组的内容并使用
lsort
-stride 2
选项:

# Convert the array to a Tcl list
set contents [array get count]

# First sort by name, as a secondary key
set contents [lsort -stride 2 -index 0 $contents]
# Then sort by count, descending, as a primary key
set contents [lsort -stride 2 -index 1 -integer -decreasing $contents]

# Print the values
foreach {name score} $contents {
    puts "$name $score"
}
-stride
选项需要Tcl 8.6


在旧版本的Tcl中,您必须将内容打包到元组的
列表中:

# Convert the array to a list of pairs
set contents {}
foreach {name score} [array get count] {
    lappend contents [list $name $score]
}

# Do the sorting
set contents [lsort -index 0 $contents]
set contents [lsort -index 1 -integer -decreasing $contents]

# Print the values
foreach pair $contents {
    # Unpack; *not* needed here, but useful for anything more complicated
    foreach {name score} $pair break
    # You could use “lassign $pair name score” but you're on 8.4
    puts "$name $score"
}
请注意,Tcl 8.4是不受支持的软件,甚至不存在安全问题,而且8.5只剩下一两年的延长支持期。我们握着人们的手的时间是有限制的…

Tcl的数组总是未排序的,事实上,当您添加元素时(当重建底层哈希表时),元素的顺序会不时变化。要获得所需的输出,最好获取数组的内容并使用
lsort
-stride 2
选项:

# Convert the array to a Tcl list
set contents [array get count]

# First sort by name, as a secondary key
set contents [lsort -stride 2 -index 0 $contents]
# Then sort by count, descending, as a primary key
set contents [lsort -stride 2 -index 1 -integer -decreasing $contents]

# Print the values
foreach {name score} $contents {
    puts "$name $score"
}
-stride
选项需要Tcl 8.6


在旧版本的Tcl中,您必须将内容打包到元组的
列表中:

# Convert the array to a list of pairs
set contents {}
foreach {name score} [array get count] {
    lappend contents [list $name $score]
}

# Do the sorting
set contents [lsort -index 0 $contents]
set contents [lsort -index 1 -integer -decreasing $contents]

# Print the values
foreach pair $contents {
    # Unpack; *not* needed here, but useful for anything more complicated
    foreach {name score} $pair break
    # You could use “lassign $pair name score” but you're on 8.4
    puts "$name $score"
}

请注意,Tcl 8.4是不受支持的软件,甚至不存在安全问题,而且8.5只剩下一两年的延长支持期。我们握住人们的手的时间是有限的…

Tcl<8.6的解决方案:

给定

把我们分类的方法是

set L [list]
foreach {k v} [array get count] {
    lappend L [list $k $v]
}
foreach e [lsort -index 1 -decreasing -integer $L] {
    lassign $e k v
    puts "$k $v"
}
说明:

  • 使用
    array Get
    获取交错键和值的平面列表
  • 从中生成一个键/值对列表—一个列表列表
  • 给定该列表,使用传递该列表的
    lsort
    命令对其进行排序
    -index 1
    选项使
    lsort
    解释 列出它作为列表排序,并在索引1处使用它们的元素 (第二个位置)用于排序
  • 要打印出已排序列表的元素,需要提取 键和从每个键返回的值。最简单的方法是使用
    lassign
    但是如果您的Tcl<8.5,您可以使用
    foreach{k v}$e break
    使用技巧或直接访问元素
    lindex$e 0
    lindex$e 1
    分别获取键和值

  • Tcl<8.6的解决方案:

    给定

    把我们分类的方法是

    set L [list]
    foreach {k v} [array get count] {
        lappend L [list $k $v]
    }
    foreach e [lsort -index 1 -decreasing -integer $L] {
        lassign $e k v
        puts "$k $v"
    }
    
    说明:

  • 使用
    array Get
    获取交错键和值的平面列表
  • 从中生成一个键/值对列表—一个列表列表
  • 给定该列表,使用传递该列表的
    lsort
    命令对其进行排序
    -index 1
    选项使
    lsort
    解释 列出它作为列表排序,并在索引1处使用它们的元素 (第二个位置)用于排序
  • 要打印出已排序列表的元素,需要提取 键和从每个键返回的值。最简单的方法是使用
    lassign
    但是如果您的Tcl<8.5,您可以使用
    foreach{k v}$e break
    使用技巧或直接访问元素
    lindex$e 0
    lindex$e 1
    分别获取键和值

  • 我使用的版本是TCL 8.4。“你能帮我一把吗?”user42556补充道;它首先需要打包到一个元组列表中。谢天谢地,您没有使用8.3或之前的版本,因为这些版本缺少
    -index
    选项,这使得所有这些都变得更加烦人……我使用的版本是TCL 8.4。“你能帮我一把吗?”user42556补充道;它首先需要打包到一个元组列表中。谢天谢地,您没有使用8.3或更早版本,因为这些版本缺少
    -index
    选项,这让所有这些都变得更加烦人…