Returning arrays from Procedures in TCL

爱⌒轻易说出口 提交于 2019-12-31 02:54:09

问题


I want to pass array and return array from a procedure, the following is the sample code i tried. But getting some errors..

set a(0) "11"
set a(1) "10"
set a(2) "20"
set a(3) "30"
set a(4) "40"

proc deleten somet {
    upvar $somet myarr
    for { set i 1} { $i < [array size myarr]} { incr i} {
        set arr($i) $myarr($i)
    }
    return arr
}

array set some[array get [deleten a]]
parray some

when i run this code i get the following error wrong # args: should be "array set arrayName list". I'm pretty sure that i dont want to use list, how can i set the array returned from the proc to another array???


回答1:


The step you were missing is that you return [array get arr] rather than just arr.

The following snippet works here

set a(0) "11"
set a(1) "10"
set a(2) "20"
set a(3) "30"
set a(4) "40"

proc deleten somet {
   upvar $somet myarr
   for { set i 1} { $i < [array size myarr]} { incr i} {
       set arr($i) $myarr($i)
   }
   return [array get arr]
} 

array set some  [deleten a]
parray some

See How to pass arrays for further information.



来源:https://stackoverflow.com/questions/9755182/returning-arrays-from-procedures-in-tcl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!