• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Windows Powershell 访问数组
    POST TIME:2021-10-18 15:42

    数组的元素可以使用索引寻址,第一个元素的索引为0,第i个元素的索引为i-1,最后一个元素的索引为Count-1,但是Powershell为了使用方便,直接可以将 -1 作为最后的一个元素的索引。

    PS C:Powershell> $books="元素1","元素2","元素3"
    PS C:Powershell> $books[0]
    元素1
    PS C:Powershell> $books[1]
    元素2
    PS C:Powershell> $books[($book.Count-1)]
    元素3
    PS C:Powershell> $books[-1]
    元素3


    从数组中选择多个元素

    PS C:Powershell> $result=ls
    PS C:Powershell> $result[0,3,5,12]
      Directory: C:Powershell
    
    Mode        LastWriteTime   Length Name
    ----        -------------   ------ ----
    d----    2011/11/23   17:25      ABC
    -a---    2011/11/24   20:04   26384 a.txt
    -a---    2011/11/24   20:27   12060 alias.ps1
    -a---    2011/11/24   17:37    7420 name.html
    


    将数组逆序输出

    PS C:Powershell> $books="元素1","元素2","元素3"
    PS C:Powershell> $books[($books.Count)..0]
    元素3
    元素2
    元素1


    给数组添加和删除元素

    因为Powershell数组在内存中是顺序存储的,所以数组的大小必须是确定的,这样才方便分配存储空间,所以给数组增加元素其实相当于创建一个新的数组,只不过之后会把原来的副本删除。在当前数组追加元素可以使用“+=”操作符。

    PS C:Powershell> $books="元素1","元素2","元素3"
    PS C:Powershell> $books+="元素4"
    PS C:Powershell> $books
    元素1
    元素2
    元素3
    元素4


    要删除第三个元素可是使用:

    PS C:Powershell> $num=1..4
    PS C:Powershell> $num
    1
    2
    3
    4
    PS C:Powershell> $num=$num[0..1]+$num[3]
    PS C:Powershell> $num
    1
    2
    4
    
    您可能感兴趣的文章:
    • Windows Powershell 创建数组
    • Windows Powershell 复制数组
    • Windows Powershell强类型数组
    • Windows Powershell使用哈希表
    • Windows Powershell使用管道
    • Windows Powershell对象转换成文本
    上一篇:PowerShell小技巧之同时使用可选强制参数
    下一篇:Windows Powershell 复制数组
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信