Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Replacing part of a element in an Array

$
0
0

Hello

I woundered if someone could please give me some advice on the following.

Is there an easier way to achieve the following

$b = $null

$A = "AXA","BZB","CYC"

foreach ($element in $A)

{

switch -wildcard ($element)

{  "*X*" {$element = $element -replace "X","T";continue}   }

[array]$b += $element

}


$A = $B

So basically I am looking for any element in Array $A which contains an "X" character and if seen replace the X with a T and put the results in Array B, so $B looks like this

ATA
BZB
CYC

Then, $A = $B makes $A the same as $B (e.g. my X has been replaced with T)

I was thinking may be I could use the $A -Contains operator using a wildcard or regex statement e.g. $A -contains *X* no joy.

I just wondered if there was a simpler way

Thanks All

Ernie

 


Viewing all articles
Browse latest Browse all 8583

Trending Articles