茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671956 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_610.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

微軟帝國 : [轉貼]Adding and Removing Items from a PowerShell Array

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Adding and Removing Items from a PowerShell Array

Adding and Removing Items from a PowerShell Array

Adding and removing Items from a PowerShell array is a topic which can lead to some confusion, so here are a few tips for you.

Create an array and we will note the type System.Array:



$Fruits = "Apple","Pear","Banana","Orange"
$Fruits.GetType()

However, if we try to Add or Remove items to the array we get errors that the “Collection was of a fixed size”



$Fruits.Add("Kiwi")
$Fruits.Remove("Apple")
$Fruits.IsFixedSize

We can see that the array we originally created is of a fixed size. The MSDN page for the ISFixedSize Property helps to explain this. Note it supports the modification of existing elements, but not the addition or removal of others.

One way to deal with this is to use a
System.Collections.ArrayList instead



[System.Collections.ArrayList]$ArrayList = $Fruits
$ArrayList.GetType()

Now if we try the previous add and remove methods we are successful:



$ArrayList.Add("Kiwi")
$ArrayList
$ArrayList.Remove("Apple")
$ArrayList

Alternatively, if we had stuck with the original Array object we could do the following to ‘add’ an item to it. (Actually it creates a new array with an additional item)



$New = $Fruits += "Kiwi"
$New
$New.GetType()

However, we can’t remove items in this way:



$New2 = $Fruits -= "Apple"

We could instead do this:



$New3 = $Fruits -ne "Apple"
$New3

 

Update: Thanks to Rob Campbell for pointing out another way to do this.

Taking your initial array you can convert it to a System.Collections.ObjectModel.Collection`1 like this, which may be easier to remember than using the full type name of either a collection or array list:



$Collection = {$Fruits}.Invoke()
$Collection
$Collection.GetType()

Now we can add and remove items using the Add and Remove methods:



$Collection.Add("Melon")
$Collection
$Collection.Remove("Apple")
$Collection


原文出處: Adding and Removing Items from a PowerShell Array | Jonathan Medd's Blog
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]How to create an ArrayList from an Array in PowerShell?
How to create an ArrayList from an Array in PowerShell?

I've got a list of files in an array. I want to enumerate those files, and remove specific files from it. Obviously I can't remove items from an array, so I want to use an ArrayList. But the following doesn't work for me:
$temp = Get-ResourceFiles
$resourceFiles = New-Object System.Collections.ArrayList($temp)

Where $temp is an Array.

How can I achieve that?



I can't get that constructor to work either. This however seems to work:
# $temp = Get-ResourceFiles
$resourceFiles = New-Object System.Collections.ArrayList($null)
$resourceFiles.AddRange($temp)

You can also pass an integer in the constructor to set an initial capacity.

What do you mean when you say you want to enumerate the files? Why can't you just filter the wanted values into a fresh array?

Edit:

It seems that you can use the array constructor like this:
$resourceFiles = New-Object System.Collections.ArrayList(,$someArray)

Note the comma. I believe what is happening is that when you call a .NET method, you always pass parameters as an array. PowerShell unpacks that array and passes it to the method as separate parameters. In this case, we don't want PowerShell to unpack the array; we want to pass the array as a single unit. Now, the comma operator creates arrays. So PowerShell unpacks the array, then we create the array again with the comma operator. I think that is what is going on.



Probably the shortest version:
[System.Collections.ArrayList]$someArray

It is also faster because it does not call relatively expensive New-Object.

原文出處:How to create an ArrayList from an Array in PowerShell? - Stack Overflow
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]A gotcha of using .NET collections (ArrayList, List,..) in PowerShell
A gotcha of using .NET collections (ArrayList, List,..) in PowerShell

I just realized this and I've learned it the hard way, so I want to share. Consider you have the following list of strings:
>$list = New-Object -TypeName System.Collections.Generic.List[string]
>$list.Add("x")
>$list.Add("yy")
>$list.Add("zzz")
>$list
x
yy
zzz

Now if you want to get the number of items in the list, you would access the .Count property, and it gives you "3" as expected.
>$list.Count
3

However, sometimes you would make mistake by accessing .Length instead of .Count. In other languages, like Python or C#, you would get an error saying this property is not available. But in PowerShell, it turns out that, if it's not found on that object, it iterates through the contained objects and access the property on them. So you end up having a list of Lengthes! Surprise!!
>$list.Length
1
2
3

You can even invoke methods!
>$list.ToUpper()
X
YY
ZZZ

Note that, as I've tried, this "feature" also works for System.Collections.ArrayList. But it does not work for PowerShell's builtin array type.

What is this "feature" called? And why is it designed such way? It's very surprising and error-prone.



Keith Hill mentioned in the comment that this is a new feature called Member Enumeration in V3. Refer to this MSDN article.

原文出處:A gotcha of using .NET collections (ArrayList, List,..) in PowerShell - Stack Overflow
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|