在Snail Language 0.171中,我将原先的线性数组改成了相关联数组,它的本质就是Hash Table,采用C#的Dictionary类型来实现。相关联数组的使用方法:
a = [100, name:"haz", language:"snail", year:2014, version:0.17, 3.14:"浮点数测试"]
数组内的元素可以以单个Value,或者Key : Value组合的方式出现,如a = [100, name:"haz"]。单个Value出现时,Snail给它一个默认的Key,Key的数值为从0开始的索引,如100的Key为0。Key只能是数字(整数、浮点数)或者字符串,上例中的name:"haz",和"name":"haz"是等同的。在初始化时,Key可以不包含双引号,Snail将没有双引号的标识当做字符串处理。如果Key是数字是,则仍然作为数字使用,如100不等同于"100"。但是其他情况下(除了初始化)必须明确的包含双引号,否则会被视为一个变量。
辅助实现功能:
1)用HasKey判断数组内是否含有所求的关键字。如果包含返回true,否则为false。
2)用foreach遍历数组。其语法为:foreach(k, v in a) { ...} 或者 foreach(k in a) { ... }
实例:
例子1:
a = [100, name:"haz", language:"snail", year:2014, version:0.17, 3.14:"浮点数测试"] print(a[0], " ", a["name"], " ", a["year"], "\n") a["test"] = "Hello Snail Language" if(a.HasKey("test"))print(a["test"], "\n") if(a.HasKey("china"))print(a[china]) if(a.HasKey(3.14)) print(a[3.14])
结果1:
100 haz 2014 Hello Snail Language 浮点数测试
例子2:
a = [100, name:"haz", language:"snail", year:2014, version:0.17, 3.14:"浮点数测试"] foreach(k, v in a) { print(k, " ", v, "\n") }
结果2:
0 100 name haz language snail year 2014 version 0.17 3.14 浮点数测试
下载:
http://download.csdn.net/detail/u012813593/6852471
[NEW] http://download.csdn.net/detail/u012813593/6852599