Removing the First Item from an Array in PowerShell
This is a quickie, but I often find myself working with arrays of objects when I'm working on a PowerShell script. Occasionally, I need to remove the first item from that array, which I've always done by using the array index: $array = $array[1..($array.count - 1)]. That certainly works, but it's also kindof ugly.
Today, I learned a better way! PowerShell does a neat trick with commas, the equals sign, and arrays. If you're assigning an array to a comma delimited list of variables, the array automatically gets split between those variables. I've used that in the past in situations like this:
$IPAddress = "192.168.0.1"
$First,$Second,$Third,$Fourth=$IPAddress.split(".")
And, as you'd expect, that will split the IP Address into octets, with each one going into its named variable. That's great, but what happens if you've got this situation?
$IPAddress = "192.168.0.1"
$First,$Second=$IPAddress.split(".")
There are 4 elements in that array, but only 2 variables to hold them! Well, $First is 192, like you'd expect... and $Second is an array of 3 integers: 168,0, and 1! That's right, by using commas to list a bunch of variables, you can trivially strip out the first item from an array.
When I'm doing this type of thing, it's usually because I need to do some operations on that first item, so my syntax is something like this:
$thisObject,$array = $array | sort CriticalColumn -Descending
That will sort my array by its CriticalColumn (with the highest value first), take that item with that highest value and store it in $thisObject (so that I can do whatever needs doing), then will overwrite the original array with the sorted remaining objects. Nice and easy!
Today, I learned a better way! PowerShell does a neat trick with commas, the equals sign, and arrays. If you're assigning an array to a comma delimited list of variables, the array automatically gets split between those variables. I've used that in the past in situations like this:
$IPAddress = "192.168.0.1"
$First,$Second,$Third,$Fourth=$IPAddress.split(".")
And, as you'd expect, that will split the IP Address into octets, with each one going into its named variable. That's great, but what happens if you've got this situation?
$IPAddress = "192.168.0.1"
$First,$Second=$IPAddress.split(".")
There are 4 elements in that array, but only 2 variables to hold them! Well, $First is 192, like you'd expect... and $Second is an array of 3 integers: 168,0, and 1! That's right, by using commas to list a bunch of variables, you can trivially strip out the first item from an array.
When I'm doing this type of thing, it's usually because I need to do some operations on that first item, so my syntax is something like this:
$thisObject,$array = $array | sort CriticalColumn -Descending
That will sort my array by its CriticalColumn (with the highest value first), take that item with that highest value and store it in $thisObject (so that I can do whatever needs doing), then will overwrite the original array with the sorted remaining objects. Nice and easy!
Comments
Post a Comment
Sorry guys, I've been getting a lot of spam recently, so I've had to turn on comment moderation. I'll do my best to moderate them swiftly after they're submitted,