How can I pass a dictionary with many arguments to proc in tcl?

proc test {a b c } {
       puts $a
       puts $b
       puts $c
}
set test_dict [dict create a 2 b 3 c 4 d 5]

      

Now I want to pass a dict to a test like this:

test $test_dict

      

How do I test

select only three elements in a dict with the same name for its parameters (keys). The expected output should be:

2
3
4

      

Because he selects a b c

in the dictionary, but not d

. How can i do this? I saw some code really like it, but I can't seem to get it to work.

+3


source to share


1 answer


I think you should be using dict get

:

proc test {test_dic} {
  puts [dict get $test_dic a]
  puts [dict get $test_dic b]
  puts [dict get $test_dic c]
}

set test_dict [dict create a 2 b 3 c 4 d 5]
test $test_dict

      

Edit: Another option would be to use dict with

:



proc test {test_dic} {
  dict with test_dic {
    puts $a
    puts $b
    puts $c
  }
}

set test_dict [dict create a 2 b 3 c 4 d 5]
test $test_dict

      

But test

gets the list.

+5


source







All Articles