Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning arrays from Procedures in TCL

Tags:

tcl

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???

like image 252
user1270123 Avatar asked Dec 28 '22 04:12

user1270123


1 Answers

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.

like image 176
Appleman1234 Avatar answered Feb 05 '23 07:02

Appleman1234