Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerTip: The Finer Points of Finessing an Array

$
0
0

Summary: Learn how to replace items in an array and how to sort an array.

Hey, Scripting Guy! QuestionI need to replace the “2” with “12” in the $array variable shown here:

$array = "1","2","3","4"

How can I do this?

 Hey, Scripting Guy! Answer

    1. $array=[regex]::replace($array,"2","12")
    2. $array = $array -replace "2","12"
    3. $array.SetValue("12",1)

Hey, Scripting Guy! QuestionI have an array defined in the $array variable shown here.

$array = 2,5,9,12,3,5

What is the easiest way to sort the array?

Hey, Scripting Guy! Answer 

1. Sort the array by using the sort static method from the [array] .NET Framework class.

[array]::sort($array)

2. Pipe the array to the Sort-Object cmdlet, and store the results back in the $array variable.

 $array = $array | sort

 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>