Windows Powershell 訪問數組_PowerShell

來源:互聯網
上載者:User

數組的元素可以使用索引定址,第一個元素的索引為0,第i個元素的索引為i-1,最後一個元素的索引為Count-1,但是Powershell為了使用方便,直接可以將 -1 作為最後的一個元素的索引。

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


從數組中選擇多個元素

PS C:Powershell> $result=lsPS C:Powershell> $result[0,3,5,12]  Directory: C:PowershellMode        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..4PS C:Powershell> $num1234PS C:Powershell> $num=$num[0..1]+$num[3]PS C:Powershell> $num124
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.