开发者

TCL custom sorting

开发者 https://www.devze.com 2023-03-04 12:41 出处:网络
I would appreciate any help on this as I am new to TCL. I created a list of strings by doing a \'regexp -all -line -inline\' + criterion on the output of a CLI command. Each element of this list now e

I would appreciate any help on this as I am new to TCL. I created a list of strings by doing a 'regexp -all -line -inline' + criterion on the output of a CLI command. Each element of this list now ends with a number and I want to sort the list on this particular numeric ending in each string but preserve the rest of the st开发者_StackOverflow中文版ring. A close example would be to have to sort the output of the 'ls -la' command by the size of the files. I tried the following but it did not work:

lsort -command  "regexp -lineanchor {\d+$}" -integer $list

After spending a day on trying to figure this out I decided to ask you guys. Would you be able to help?


To be honest, I don't understand what you want to achieve with -command "regexp -lineanchor {\d+$}". The command regexp -lineanchor {\d+$} should actually always return 1 if you plan to compare numbers.

If you want to sort a list by the last element of its sublists you can use the -index option. E.g.:

lsort -index end -integer {{x y 5} {a b 8} {c c 3} {u u 1} {x y 2}}

returns:

{u u 1} {x y 2} {c c 3} {x y 5} {a b 8}

If you don't have your data in sublists but have the data line by line you have to split it before, e.g.:

lsort -index end -integer [split $data "\n"]
0

精彩评论

暂无评论...
验证码 换一张
取 消