VBS基础篇 - Dictionary对象

Dictionary是存储数据键和项目对的对象,其主要属性有Count、Item、Key,主要方法有Add、Exists、Items、Keys、Remove、RemoveAll。

'建立字典
Dim Dict : Set Dict = CreateObject("Scripting.Dictionary")

'添加键值对
Dict.Add "Key1", "Item1"
Dict.Add "Key2", "Item2"
Dict.Add "Key3", "Item3"

'字典中键值对数量
WScript.Echo "字典中现有键值对数量: " & Dict.Count '让一个脚本在屏幕上显示文本信息

WScript.Echo 

'检查指定键是否存在
If Dict.Exists("Key1") Then
    WScript.Echo "Key1 存在!"
Else
    WScript.Echo "Key1 不存在!"
End If

If Dict.Exists("Keyn") Then
    WScript.Echo "Keyn 存在!"
Else
    WScript.Echo "Keyn 不存在!"
End If

WScript.Echo 

'遍历字典
Sub TraverseDict
    Dim DictKeys, DictItems, Counter
    DictKeys = Dict.Keys
    DictItems = Dict.Items 'Items返回一个包含所有Item值的数组
    For Counter = 0 To Dict.Count - 1 'Count返回Dictionary对象键数目
        WScript.Echo _
            "键: " & DictKeys(Counter) & _ '& 字符串连接运算符
            "值: " & DictItems(Counter)
    Next
End Sub

TraverseDict

WScript.Echo 

'在一个键值对中,修改键或修改值
Dict.Key("Key2") = "Keyx"
Dict.Item("Key1") = "Itemx"
TraverseDict

WScript.Echo 

'删除指定键
Dict.Remove("Key3")
TraverseDict

WScript.Echo 

'删除全部键
Dict.RemoveAll
WScript.Echo "字典中现有键值对数量: " & Dict.Count

 

运行结果截图:

VBS 脚本中的字典.动态数组.队列和堆栈

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值