Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
Linux 如何根据TCL中的lindex值对不同列表进行排序_Linux_Shell_Scripting_Tcl - Fatal编程技术网

Linux 如何根据TCL中的lindex值对不同列表进行排序

Linux 如何根据TCL中的lindex值对不同列表进行排序,linux,shell,scripting,tcl,Linux,Shell,Scripting,Tcl,如何根据lindex值对不同列表进行排序请帮助我 Qstn: 假设我有一个数组变量调用x 设置$x(1)“4 5 7” 设置$x(2)“1 3 9” 设置$x(3)“9 1 5” 我想按照列表中第一个元素的排序顺序对数组进行排序 lindex$x(2)0

如何根据lindex值对不同列表进行排序请帮助我

Qstn:

假设我有一个数组变量调用x

设置$x(1)“4 5 7”

设置$x(2)“1 3 9”

设置$x(3)“9 1 5”

我想按照列表中第一个元素的排序顺序对数组进行排序

lindex$x(2)0

所以这里我需要的答案是一个新变量,比如$keys\u sortd

echo$keys_排序

2 1 3

我希望我的问题是清楚的。请帮我找到最简单的方法

set x(1) "4 5 7"
set x(2) "1 3 9"
set x(3) "9 1 5"
set l [array get x]
puts $l
set sorted [lsort -stride 2 -index 1 $l]
puts $sorted
foreach {k v} $sorted {
        lappend result $k
}
puts $result

% tclsh8.6 sort.tcl
1 {4 5 7} 2 {1 3 9} 3 {9 1 5}
2 {1 3 9} 1 {4 5 7} 3 {9 1 5}
2 1 3
希望有帮助;)

编辑:8.6之前的代码

foreach {k v} [array get x] {
        lappend unsorted [list $k [lindex $v 0]]
}
puts $unsorted
set sorted [lsort -index 1 $unsorted]
puts $sorted
foreach i $sorted {
        lappend result [lindex $i 0]
}
puts $result
希望有帮助;)

编辑:8.6之前的代码

foreach {k v} [array get x] {
        lappend unsorted [list $k [lindex $v 0]]
}
puts $unsorted
set sorted [lsort -index 1 $unsorted]
puts $sorted
foreach i $sorted {
        lappend result [lindex $i 0]
}
puts $result

@siyb有TCL8.6的答案。对于8.5:

% foreach {key value} [array get x] {lappend y [list $key $value]}
% set y
{1 {4 5 7}} {2 {1 3 9}} {3 {9 1 5}}
% lsort -index {1 0} $y
{2 {1 3 9}} {1 {4 5 7}} {3 {9 1 5}}
% foreach elem [lsort -index {1 0} $y] {lappend sorted_keys [lindex $elem 0]}
% set sorted_keys
2 1 3

@siyb有TCL8.6的答案。对于8.5:

% foreach {key value} [array get x] {lappend y [list $key $value]}
% set y
{1 {4 5 7}} {2 {1 3 9}} {3 {9 1 5}}
% lsort -index {1 0} $y
{2 {1 3 9}} {1 {4 5 7}} {3 {9 1 5}}
% foreach elem [lsort -index {1 0} $y] {lappend sorted_keys [lindex $elem 0]}
% set sorted_keys
2 1 3