Turbo-Charging Arrays

Simple arrays have no built-in mechanism to insert new elements or extract elements at given positions. For example, to extract the 5. element from an array, in PowerShell you'd have to work around it like this:

$array = 1..10
$array = $array[0..3] + $array[5..9]
$array

This is inconvienent and slow. A much better way is to "upgrade" your array to an arraylist. Arraylists are more sophisticated arrays that have methods like RemoveAt() and InsertAt():

$array = 1..10
$betterarray = [System.Collections.ArrayList]$array
$betterarray.RemoveAt(4)
$betterarray
$array = $betterarray.toArray()
$array

Posted Feb 17 2009, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.