想要实现类似于dict的功能

时间:2015-08-03 17:16:05

标签: dictionary tcl

我是TCL的新手。我想创建具有字符串键的TCL字典结构。我想计算字典中某些类型的出现次数,因此我想在特定索引处更新字典(索引是字符串)。怎么做到这一点?

  

示例:(逻辑上没有精确的tcl语法)

     

a ['hello'] = 0,   a ['hi'] = 0
  现在如果在我正在扫描的数据中找到你好,我想   更新['hello'] = a ['hello'] + 1;

请帮我用语法来实现这一目标。我使用的tcl版本低于8.5,不支持dict。

2 个答案:

答案 0 :(得分:1)

dict incr dictionaryVariable key?increment?

  

这会添加给定的增量值(如果是,则为默认值为1的整数)   未指定)给定键映射到的值   包含在给定变量中的字典值,写入   结果字典值返回该变量。不存在的密钥   被视为映射为0。增加值是一个错误   对于现有密钥,如果该值不是整数。更新了   返回字典值。

% set myinfo {firstName Dinesh lastName S age 25}
firstName Dinesh lastName S age 25
% dict incr myinfo age; # This will increase the value of 'age' by 1 which is default
firstName Dinesh lastName S age 26
% dict incr myinfo age 10; # OMG!!!, I'm old already...
firstName Dinesh lastName S age 36
% dict incr myinfo count; # Using non-existing key name will create a key with that name
firstName Dinesh lastName S age 36 count 1
% dict incr myinfo count -1; # Even negative values can be used
firstName Dinesh lastName S age 36 count 0
%

参考: dict incr

答案 1 :(得分:1)

我不知道从8.5版开始支持字典。所以我无法在我的脚本中使用dict命令。

因此我正在使用关联数组(HashMaps)。

示例:

B

迭代关联数组:

set a(hello)0
set a(hi)    0

set a(hello) [expr $a(hello) + 1]
相关问题