J.S
J.S

Reputation: 47

Accessing variable name inside proc in tcl

I am trying to create a proc that lists all elements of an array:

Here is the proc:

proc array_values {array_input} {
  upvar $array_input arr
  foreach name [lsort [array names arr]] {puts "arr($name) : $arr($name)"}
}

When I call the proc with this arguments:

array_values CMN_CLK_DIG_PATH

I get the output:

arr(0) : u_sm/u_sm_n/u_sm_ph/u_chip/Icmn0/
arr(1) : u_sm/u_sm_n/u_sm_ph/u_chip/Icmn1/

Instead, I want the output as:

CMN_CLK_DIG_PATH(0) : u_sm/u_sm_n/u_sm_ph/u_chip/Icmn0/
CMN_CLK_DIG_PATH(1) : u_sm/u_sm_n/u_sm_ph/u_chip/Icmn1/

Any idea how to do that?

Upvotes: 1

Views: 61

Answers (1)

sharvian
sharvian

Reputation: 654

The variable array_input is still valid in your proc. Its value is string "CMN_CLK_DIG_PATH". So simply replace arr with $array_input :

foreach name [lsort [array names arr]] {puts "$array_input\\($name\\) : $arr($name)"}

Upvotes: 1

Related Questions