2

I'm trying to remove the leading 0's in all values of an array within a two dimensional array.

Is there a library that can do this for me? I tried it with a foreach loop, but it only trims the current temp variable.

Here is the code:

[String[]]$arr1 = @('str1','str2','str3')
[String[]]$arr2 = @('str4','str5','str6')
[String[]]$arr3 = @('0str7')

[System.Object[]]$twoDimensionalArray = @()
[System.Object[]]$twoDimensionalArray += ,($arr1)
[System.Object[]]$twoDimensionalArray += ,($arr2)
[System.Object[]]$twoDimensionalArray += ,($arr3)

Write-Host "Removing leading zeros from array in twoDimentionalArray..."
ForEach ($strValue in $twoDimensionalArray[2])
{
    $strValue = $strValue.TrimStart('0')
}
Write-Host "Leading zero's are removed."
Write-Host ""

Write-Host '$strValue =' $strValue
Write-Host '$twoDimensionalArray[2] =' $twoDimensionalArray[2]
Write-Host ""

Output (0str7)

Removing leading zeros from array in twoDimentionalArray...
Leading zero's are removed.

$strValue = str7
$twoDimensionalArray[2] = 0str7

Desired Output (str7)

Removing leading zeros from array in twoDimentionalArray...
Leading zero's are removed.

$strValue = str7
$twoDimensionalArray[2] = str7

Any ideas?

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • See this answer as to why what you are doing is not a two dimensional array http://stackoverflow.com/a/9397385/3829407 – Matt Apr 21 '16 at 10:43

2 Answers2

1

Here is the modified code snippet with the desired output. The trick is to modify the object itself:

    [String[]]$arr1 = @('str1','str2','str3')
    [String[]]$arr2 = @('str4','str5','str6')
    [String[]]$arr3 = @('0str7')

    [System.Object[]]$twoDimensionalArray = @()
    [System.Object[]]$twoDimensionalArray += ,($arr1)
    [System.Object[]]$twoDimensionalArray += ,($arr2)
    [System.Object[]]$twoDimensionalArray += ,($arr3)

    Write-Host "Removing leading zeros from array in twoDimentionalArray..."
    [int] $arrayCounter = 0
    ForEach ($strValue in $twoDimensionalArray[2])
    {
        $twoDimensionalArray[2][$arrayCounter] = $strValue.TrimStart('0')

        $arrayCounter++
    }
    Write-Host "Leading zero's are removed."
    Write-Host ""

    Write-Host '$strValue =' $strValue
    Write-Host '$twoDimensionalArray[2] =' $twoDimensionalArray[2]
    Write-Host ""

I have tested this and it outputs str7 as you are expecting.

Aman Sharma
  • 1,930
  • 1
  • 17
  • 31
0

Edit all of the jagged array

This might be considered cheating and I don't know if you actual use case will abide by this but I tried to make it a little easier by using an array operator on this jagged array.

$twoDimensionalArray = $twoDimensionalArray | Foreach-object{
    ,($_ -replace "^0+")
}

This should save back to the original array $twoDimensionalArray. Using -replace save us running a separate loop to going inside the other array.

What this will do is use regex to replace all leading zeros from the array elements. Since the array would then be unrolled we still need the unary operator to ensure that the data is sent down the pipe as an array.

Depending on what control you have on your input this could be simplified as

$arr1 = @('str1','str2','str3')
$arr2 = @('str4','str5','str6')
$arr3 = @('0str7')

[string[]]$twoDimensionalArray = $arr1, $arr2, $arr3 | ForEach-Object{
    ,($_ -replace "^0+")
} 

Edit only one element of the jagged array

Very simple change to get that running using the above logic.

$elementToChange = 2
$twoDimensionalArray[$elementToChange] = [String[]]@($twoDimensionalArray[$elementToChange] -replace "^0+")
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Even if you don't mention explicitly, -replace also parses all the elements in a loop, which essentially takes the same performance. In my opinion It depends on the requirement whether or not to use -replace vs the loop. If you want more control and want to perform more operations then loop makes more sense. – Aman Sharma Apr 21 '16 at 02:32
  • @AmanSharma I thought I was explicit _ I tried to make it a little easier by using an __array operator__ on this jagged array_. If you want to perform more operations the you can chain more `-replace`s or use a more robust regex. I do understand your point. – Matt Apr 21 '16 at 03:08
  • I only wanted to strip the 0's from the 3rd array (not all arrays in the two dimensional array), but thanks anyway. – Fiddle Freak Apr 21 '16 at 04:26
  • @FiddleFreak My bad for that. I have updated my answer so that it now actually answers your question. – Matt Apr 21 '16 at 10:41
  • Hi @Matt, what is the purpose of the comma in `,($_ -replace "^0+")` ? – Zacharious Feb 07 '17 at 08:10
  • It's in the answer where I refer to the unary operator. Forgot the word comma – Matt Feb 07 '17 at 12:12