user3813234
user3813234

Reputation: 1682

replace several values at once in a dict in tcl

I have a dict with several elements. I want to replace several values at once.

This is how it has worked:

 proc  myproc  {param} {


      set tempDict [dict replace $param "fd" "gfdgfdgf"]
      set tempDict2 [dict replace $tempDict "fds" "gfdgf"]
      set tempDict3 [dict replace $tempDict2 "fsdf" "gdfg7"]
      set tempDict4 [dict replace $tempDict3 "ztrzrt" "gdfgf"]

      puts "\n"
      puts $tempDict4

  }

What's the right way to do this? The way I understand the documentation, dict replace returns a copy of the altered dict. But my code surely cannot be the right way to do this.

Upvotes: 0

Views: 1185

Answers (2)

Roman Mishin
Roman Mishin

Reputation: 501

Have a look at dict merge command. The values of the last specified dictionary will take precedence:

% dict merge {a 1 b 2} {a 11 c 33}
a 11 b 2 c 33

Upvotes: 1

Dinesh
Dinesh

Reputation: 16438

You can append more than one key/value pair.

% set d [ dict create user dinesh age 25 ]
user dinesh age 25
% set d [dict replace $d user Rajesh age 29]
user Rajesh age 29
% 

Upvotes: 1

Related Questions