home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / tcl / 2283 next >
Encoding:
Text File  |  1993-01-04  |  1.3 KB  |  41 lines

  1. Newsgroups: comp.lang.tcl
  2. Path: sparky!uunet!psinntp!sugar!karl
  3. From: karl@NeoSoft.com (Karl Lehenbauer)
  4. Subject: Re: How do I return an array as a list ?
  5. Organization: NeoSoft Communications Services -- (713) 684-5900
  6. Date: Mon, 4 Jan 1993 05:48:14 GMT
  7. Message-ID: <C0BEsF.1K2@NeoSoft.com>
  8. Keywords: array list procedure
  9. References: <brucet.726113122@extro.ucc.su.OZ.AU>
  10. Lines: 29
  11.  
  12. In article <brucet.726113122@extro.ucc.su.OZ.AU> brucet@extro.ucc.su.OZ.AU (Bruce Tulloch) writes:
  13. >proc arrayToList {array {context 1}} {
  14. >    set names [uplevel $context array names $array]
  15. >    if {![llength $names]} {return}
  16. >    foreach name $names {
  17. >        set item [echo $name [uplevel $context echo $[uplevel $context echo $array]($name)]]
  18. >        lappend contents $item
  19. >    }
  20. >    return $contents
  21. >}
  22.  
  23. Sure, use upvar.  The proc below shows how:
  24.  
  25. proc arrayToList {arrayName} {
  26.     upvar $arrayName myArray
  27.  
  28.     set result ""
  29.     foreach name [array names myArray] {
  30.     lappend result "$name $myArray($name)"
  31.     }
  32.     return $result
  33. }
  34.  
  35. Note that with upvar this sort of thing isn't so necessary, anyway, because
  36. you can use upvar to pass the names of arrays to procs, where the proc
  37. links into the actual array with upvar.
  38. -- 
  39. -- Email info@NeoSoft.com for info on getting interactive Internet access.
  40. "In a minute, I'll burp up your droid."
  41.