[Powershell] Powershell Dynamic Arrays

Source: Internet
Author: User

Or we can say C #? Since PowerShell is from C #. For sure the subject was aganist itself, according to belong page of MSDN, the Isfixedsize Arrays.

Https://msdn.microsoft.com/en-us/library/system.array.isfixedsize (v=vs.110). aspx

That means the number of elements in a single array is fixed, we can use below codes to add new elements but it actually C loned a new array and resized it, the address pointing is broken.

$t = 1..2$r = $t $r[1] = 9$t
# Output of $t is 1,9 because $r and $t pointing to the same array like pointing in c###### $r + = 10$t
# Output of $t keep 1,9 this was because a new array cloned and resized with new elements

Ways like the quite simply for everyone, but the course costs resources of system, script is a script, we need it easy for using otherwise it doesn ' t is called as script.

Like the codes above, the "the" is simply enough but broke the fact $r and $t original pointing to a same array, which I CA n Update element values in one variable, another would follow. But the adding new elements cloned a new array and different pointings.

Sometimes I really need 2 or more variables pointing to a same array update values together, communication between threads As example, at the same time I also has to add/remove elements.

People like me might is thinking there is methods from the array itself to do such requirement. The fact is true and false at the same time. See below,

Remove         method                void Ilist.remove (System.Object value) RemoveAt       Method                void ilist.removeat (int index)
# when using Remove and RemoveAt methods, errors'll be a thrown says the collection is size fixed. Clear Method static void Clear (array array, int index, int length)
# when using [Array]::clear ($t, 1, 1), yes it can clear the Vaule but the total number of elements would not be updated.

All I want are multiple varibles pointing to the same array and I can update elements number without impacting the first RU Le.

I keep my focus on array object, thought there might is wonderful ways to achieve I want, fact prove I am wrong. So I turned my face to the property isfixedsize, I want to find another object like array but the property to be false, th Is are where ArrayList comes from.

Https://msdn.microsoft.com/en-us/library/system.collections.arraylist (v=vs.110). aspx

$t = new-Object system.collections.arraylist$t. isfixedsize# output is false 

Good good, the first step achieved, it dynamic and usage like a normal array. It feeds new elements like hash table which are so friendly.

Add            Method                int Add (System.Object value), int ilist.add (System.Object v ... AddRange       method                void AddRange (System.Collections.ICollection c) Remove         method                void Remove ( System.Object obj), void Ilist.remove (System.ob ... RemoveAt       method                void RemoveAt (int index), void ilist.removeat (int index) removerange    Method                void RemoveRange (int index, int count) trimtosize     Method                void trimtosize () capacity       Property              int capacity {Get;set;} Count          Property              int count {get;}

Add is to add new single element at the end, AddRange copy another collection object to its end, elements adding/removing Problem solved.

The 3 ways regarding to remove is to remove elements which solve problems on removing.

Count and capacity property would increase themselves with elements adding, but capacity would not follow elements removing, Which count does, this can help on getting element.

TrimToSize is to align count and capacity, reset capacity to its count.

Until now, what I want "multiple varibles pointing to the same array and I can update elements number without impacting th E First rule "is achieved.

Yes, this is seems much easier and wonderful, how is the cost?

No matter memory or CPU it takes, it much than array, one simplies testing is open 2 Powershell.exe and use array and ARRA Ylist to build 2 new big array like 1..1000000, we can see the diffenerce from Taskmgr, CPU time was too shore to notice by Human which surely cost more. The easy and friendly is based on background C # codes, although we don ' t know it but it really exists anyway, as I said, Script is a script, automation are first priority.

[PowerShell] PowerShell dynamic arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.