1.什么是哈希表
它是一种高效的符合数据类型,可以包含变量、数组、结构体、指针、、对象、链表、哈希表等数据类型
特点
关键词与值对应,可以通过链表函数或关键字快速处理访问
2.创建哈希表
通过Hash()函数来创建,主要有四种格式
但是都是关键字keys和values对应
3.访问哈希表
访问哈希表是通过关键字来实现的
IDL> Hash_Ge=Hash("one",1.0,"blue",[255,0,0],"Pi",!DPI)
IDL> help,Hash_Ge
HASH_GE HASH <ID=1 NELEMENTS=3>
IDL> print,Hash_Ge["one"]
1.00000
IDL> print,Hash_Ge["Pi"]
3.1415927
4.哈希表添加
IDL> Hash_Ge=Hash("one",1.0,"blue",[255,0,0],"Pi",!DPI)
IDL> help,Hash_Ge
HASH_GE HASH <ID=1 NELEMENTS=3>
IDL> print,Hash_Ge["one"]
1.00000
IDL> print,Hash_Ge["Pi"]
3.1415927
IDL> Hash_Ge["black"]=100;这里添加了元素
IDL> print,Hash_Ge
one: 1.0000000
blue: 255 0 0
Pi: 3.1415926535897931
black: 100
5.哈希表的其他操作
5.1 关键字输出
可以调用Hash.Keys()
函数
IDL> Hash_Ge=Hash("one",1.0,"blue",[255,0,0],"Pi",!DPI)
IDL> list=Hash_Ge.keys()
IDL> print,list
one
blue
Pi
5.2 关键字查询
就是查询有没有这个关键字,如果有返回1,没有返回0
IDL> Hash_Ge=Hash("one",1.0,"blue",[255,0,0],"Pi",!DPI)
IDL> print,Hash_Ge.HasKey("one","white");输入错误,单个关键字查询可以这样写
% HASH::HASKEY: Incorrect number of arguments.
% Execution halted at: $MAIN$
IDL> print,Hash_Ge.HasKey(["one","white"]);正确的写法,要用数组
1 0
5.3 删除哈希表
用Hash.Remove()
5.4 转换为结构体
IDL> Hash_Ge=Hash("black",0,"grey",128,"gray",128,"white",255)
IDL> struct_Ge=Hash_Ge.ToStruct()
IDL> help,Hash_Ge
HASH_GE HASH <ID=39 NELEMENTS=4>
IDL> help,struct_Ge
** Structure <1147d470>, 4 tags, length=8, data length=8, refs=1:
WHITE INT 255
BLACK INT 0
GRAY INT 128
GREY INT 128
5.5 哈希表组合
用+
来实现
5.6 哈希表比较
比较的返回结果是表中的共同元素或不同的元素
IDL> Hash1=Hash("Key1",1,"Key2",2,"Key3",3)
IDL> Hash2=Hash("Key1",1,"Key2",2,"anatherkey",5)
IDL> result=Hash1 EQ Hash2
IDL> print,result
Key2
Key1
IDL> result=Hash1 NE Hash2
IDL> print,result
Key3
anatherkey
IDL>
5.7 销毁哈希表
Obj_Destroy
```IDL> Obj_Destroy,Hash1
IDL> print,Hash1
<ObjHeapVar60>
IDL> help,Hash1
HASH1 OBJREF = <ObjHeapVar60>