Skip to content
Tcl Source Code History Of Ticket 49d6b5d702
Login

History Of Ticket 49d6b5d702

Artifacts Associated With Ticket 49d6b5d702

  1. Ticket change [a69ac170b0] (rid 204575) by anonymous on 2022-09-22 04:53:42:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized 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"
      
    5. is_private initialized to: "0"
    6. login: "anonymous"
    7. priority initialized to: "5 Medium"
    8. resolution initialized to: "None"
    9. severity initialized to: "Cosmetic"
    10. status initialized to: "Open"
    11. submitter initialized to: "anonymous"
    12. subsystem initialized to: "18. Commands M-Z"
    13. title initialized to: "parray sort order is often not useful"
    14. type initialized to: "Bug"