Tcl -- 数组

---- 数组是一些元素的集合。Tcl的数组和普通计算机语言中的数组有很大的区别。

在Tcl中,不能单独声明一个数组,数组只能和数组元素一起声明。

数组中,数组元素的名字包含两部分:数组名数组中元素的名字,Tcl中数组元素的名字(下标)可以为任何字符串。

% set day(monday) 1
% set day(tuesday) 2

上面的命令:第一个命令生成一个名为day的数组,同时在数组中生成一个名为monday的数组元素,并把值置为1.

第二个命令生成一个名为tuesday的数组元素,并把值置为2.

set a monday
set day(monday) 1
set b $day(monday) //b 的值为1,即day(monday)的值。
set c $day($a) //c 的值为1,即day(monday)的值
---- append 和incr

---- append 命令把文本加到一个变量的后面,例如:

% set txt hello
hello
% append txt "! How are you"
hello! How are you
---- incr 命令把一个变量值加上一个整数。incr 要求变量原来的值和新加的值都必须是 整数
%set b a
a
% incr b
expected integer but got "a"
%set b 2
2
%incr b 3
5
---- TCL 中支持的数学函数如下:

abs( x) Absolute value of x.

ceil( x) Smallest integer not less than x.

floor( x) Largest integer not greater than x.

pow( x, y) x raised to the power y.

round( x) Integer value produced by rounding x.

sqrt( x) Square root of x.

---- concat命令

语法:concat list ?list...?

这个命令把多个list 合成一个list,每个list 变成新list 的一个元素。

---- lindex命令

语法:lindex list index

返回list 的第index 个(0-based)元素。例:

% lindex {1 2 {3 4}} 2
3 4

---- llength命令

语法:llength list

返回list 的元素个数。例:

% llength {1 2 {3 4}}
3

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值