|
From: KATO K. <k.k...@gm...> - 2008-05-31 17:34:47
|
I made a procedure based on "dictsort" for testcases.
Currently, I use this in modules/yaml/yaml.test
There is upward compatibility between dictsort2 and dictsort.
If dictsort2's 2nd argument is not given, as the same work of dictsort.
proc dictsort2 {dict {pattern d}} {
set cur [lindex $pattern 0]
set subs [lrange $pattern 1 end]
set out {}
if {$cur eq "l"} {
foreach {node} $dict {
if {$subs ne ""} {set node [dictsort2 $node $subs]}
lappend out $node
}
return $out
}
if {$cur ne "d"} {array set msubs $cur}
array set map $dict
foreach key [lsort [array names map]] {
set node $map($key)
if {$cur eq "d"} {
if {$subs ne ""} {set node [dictsort2 $node $subs]}
} else {
if [array exists msubs($key)] {
set node [dictsort2 $node $msubs($key)]]
}
}
lappend out $key $node
}
return $out
}
# Working Sample
% set dict {bb {h1 g1 d1 s1} aa {y2 t2 r2 e2}}
bb {h1 g1 d1 s1} aa {y2 t2 r2 e2}
# sorting only top
% dictsort2 $dict
aa {y2 t2 r2 e2} bb {h1 g1 d1 s1}
# sorting 2 ranks
% dictsort2 $dict {d d}
aa {r2 e2 y2 t2} bb {d1 s1 h1 g1}
# sorting only "aa"
% dictsort2 $dict {{aa d}}
aa {y2 t2 r2 e2} bb {h1 g1 d1 s1}
# sorting dict in list
% dictsort2 {{c1 d1 b1 a1} {j2 l2 a2 d2}} {l d}
{b1 a1 c1 d1} {a2 d2 j2 l2}
|