Overview
| Artifact ID: | a69ac170b022565e40666c82182f58a272cde6f180a8dd35fc939c2db1c1f98a |
|---|---|
| Ticket: | 49d6b5d702c34ed48539c2d1d36f542dbb79a65b
parray sort order is often not useful |
| User & Date: | anonymous on 2022-09-22 04:53:42 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/plain"
- comment changed to:
parray, a built in command, uses the default -ascii sort option which is generally the wrong choice, especially with numerical indices. The results are quite ugly, esp. with lots of values. This command is mostly for testing, esp. interactively: % foreach i {1 2 3 4 10 11 100 101 X2 X1000z x99y x1000y} {set foo($i) $i} % parray foo foo(1) = 1 foo(10) = 10 foo(100) = 100 foo(101) = 101 foo(11) = 11 foo(2) = 2 foo(3) = 3 foo(4) = 4 foo(X1000z) = X1000z foo(X2) = X2 foo(x1000y) = x1000y foo(x99y) = x99y With a -dictionary sort instead, this would be: % parray foo foo(1) = 1 foo(2) = 2 foo(3) = 3 foo(4) = 4 foo(10) = 10 foo(11) = 11 foo(100) = 100 foo(101) = 101 foo(X2) = X2 foo(x99y) = x99y foo(x1000y) = x1000y foo(X1000z) = X1000z which is likely the desired result. However, one may sometimes prefer an -ascii sort (or other types) so with just 2 changes to this small procedure: proc parray {a {pattern *} {sort -dictionary} args} { ;# default dictionary sort <<<< change upvar 1 $a array if {![array exists array]} { return -code error "\"$a\" isn't an array" } set maxl 0 set names [lsort {*}$sort {*}$args [array names array $pattern]] ;# other sort options <<< change foreach name $names { if {[string length $name] > $maxl} { set maxl [string length $name] } } set maxl [expr {$maxl + [string length $a] + 2}] foreach name $names { set nameString [format %s(%s) $a $name] puts stdout [format "%-*s = %s" $maxl $nameString $array($name)] } } then parray could take multiple sort options like so: % parray foo \[1-9]* -dict -decr foo(101) = 101 foo(100) = 100 foo(11) = 11 foo(10) = 10 foo(4) = 4 foo(3) = 3 foo(2) = 2 foo(1) = 1 % set foo(3.14) 3.14 ;# add another one 3.14 % parray foo \[0-9]* -real foo(1) = 1 foo(2) = 2 foo(3) = 3 foo(3.14) = 3.14 foo(4) = 4 foo(10) = 10 foo(11) = 11 foo(100) = 100 foo(101) = 101 An added plus is that one could verify that all the indices were integers with, % parray foo \[0-9]* -integer expected integer but got "3.14" - is_private changed to: "0"
- login: "anonymous"
- priority changed to: "5 Medium"
- resolution changed to: "None"
- severity changed to: "Cosmetic"
- status changed to: "Open"
- submitter changed to: "anonymous"
- subsystem changed to: "18. Commands M-Z"
- title changed to: "parray sort order is often not useful"
- type changed to: "Bug"