arrays - How do I get the hash name and key in TCL? -
i'm trying figure out how hash name , key in following situation. have following hash value:
set client(car) "koenigsegg"
if pass $client(car)
proc, value passed "koenigsegg". there way capture fact hash , key storing value 'client' , 'car', respectively?
for example:
proc foobar {item} { set the_item $item } foobar $client(car)
in example, proc receives value of $client(car), "koenigsegg". $item
"koenigsegg", don't know kind of item is. i'd hash name "client" , key "car" know "koenigsegg" "client car".
you can pass name of array proc, use upvar
access it:
proc process_array {arrayname} { upvar 1 $arrayname myarray puts "car $myarray(car)" } set client(car) "koenigsegg" process_array client ;# pass name of array, note: no dollar sign
output:
car koenigsegg
i hope looking for.
update
so, want pass 2 things proc: hash name (tcl refers "array") , index name (car):
proc process_array {arrayname index} { upvar 1 $arrayname myarray puts "my array $arrayname" puts "list of indices: [array names myarray]" puts "car $myarray($index)" } set client(car) "koenigsegg" process_array client car;# pass name of array, note: no dollar sign
output:
my array client list of indices: car car koenigsegg
update 2
it seems original poster (op) asking this:
process_array $client(car)
and expect proc process_array
figure out name of array (client
) , index (car
). not possible in knowledge. when tcl interpreter encounters line above, evaluates $client(car)
expression , line becomes:
process_array koenigsegg
that means within process_array
, proc has no idea array. knows has passed string "koenigsegg".
now, if pass proc name of array, can figure out array's name, any indices array has. please see previous code.
Comments
Post a Comment