Posted to tcl by aspect at Thu Jul 10 14:45:31 GMT 2014view raw

  1. proc withArray {name args} {
  2. upvar 1 $name a
  3. set script [lmap name [array names a] {
  4. list upvar 1 a($name) $name
  5. }]
  6. set script [join $script \n]
  7. append script \n[concat {*}$args] ;# mostly I do this out of superstition
  8. apply [list {} $script]
  9. }
  10.  
  11. proc test {} {
  12. set a(bar) 3
  13. set a(baz) 10
  14. withArray a {
  15. set foo [expr {$bar+$baz}]
  16. incr baz
  17. }
  18. parray a
  19. }
  20.  
  21. test
  22.  
  23. #a(bar) = 3
  24. #a(baz) = 11
  25. #a(foo) = 13
  26.