Posted to tcl by Napier at Sun Jul 06 17:53:06 GMT 2014view raw

  1. proc difflists {l1 l2} {
  2. set lcsData [struct::list longestCommonSubsequence $l1 $l2]
  3. set diffs [struct::list lcsInvert $lcsData [llength $l1] [llength $l2]]
  4. set changedKeys ""
  5. foreach d $diffs {
  6. set op ""
  7. lassign $d type i1 i2; lassign $i1 s1 e1; lassign $i2 s2 e2
  8. puts "$type: [lrange $l2 {*}$i2] to -> [lrange $l1 {*}$i1]"
  9. #:[lindex $l1 $e1] -> [lindex $l2 $s2-1]:[lindex $l2 $e2]
  10. set flatkey [lindex $l1 $s1-1]; lappend op [string map {. " "} $flatkey]
  11. lappend op [lrange $l1 {*}$i1]
  12. lappend changedKeys $op
  13. }
  14. return $changedKeys
  15. }
  16.  
  17. proc dict_flatten {dictVal} {
  18. dict for {k v} $dictVal {
  19. if {[llength $v] % 2 == 0} {
  20. set dictVal [dict remove $dictVal[set dictVal {}] $k]
  21. dict for {sk sv} [dict_flatten $v] {dict set dictVal ${k}.${sk} $sv}
  22. }
  23. }
  24. return $dictVal
  25. }
  26.  
  27. proc dictCompare {newDict oldDict} {
  28. # Compares Two Dictionaries and Returns with a List
  29. # with all changed Dictionary Keys.
  30. # Example Resonse:
  31. # {key1 key1 key1} {key1 key1 key2} {key2 key1 key1}
  32. # Get Values using the {*} operator on the response:
  33. # dict get $dict {*}[lindex $differences 0]
  34. set differences [difflists [dict_flatten $newDict] [dict_flatten $oldDict]]
  35. return $differences
  36. }
  37.  
  38.