0

I'm trying to update each value in my hashtable with a string replacement. I tried solutions mentioned here, but to no avail.

My current code generates the values with the required replacement. But how do I get them to replace the values in my hashtable?

$mactable = [ordered]@{
    "A" = "11:22:33:XX:YY:10"
    "B" = "11:22:33:XX:YY:20"
    "C" = "11:22:33:XX:YY:30"
    "D" = "11:22:33:XX:YY:40"
    "E" = "11:22:33:XX:YY:50"
}

$octet4hex = "10"
$octet5hex = "16"

foreach($value in $mactable.values){
    $value -replace "XX:YY", ($octet4hex+":"+$octet5hex)
}

I tried this (as a mentioned solution):

foreach($value in @($mactable.values)){
    $mactable[$key] = $value -replace "XX:YY", ($octet4hex+":"+$octet5hex)
}

But that gives me the following error:

Index operation failed; the array index evaluated to null.

Expected result is:

"A" = "11:22:33:10:16:10"
"B" = "11:22:33:10:16:20"
"C" = "11:22:33:10:16:30"
"D" = "11:22:33:10:16:40"
"E" = "11:22:33:10:16:50"

1 Answer 1

3

Since you're using an OrderedDictionary and it supports Item[Int32] you can do it with a for loop:

for ($i = 0; $i -lt $mactable.Keys.Count; $i++) {
    $value = $mactable[$i] -replace 'XX:YY', ($octet4hex + ':' + $octet5hex)
    $mactable[$i] = $value
}
$mactable['E'] # Outputs: 11:22:33:10:16:50

If however you were using a hashtable instead, you would need to clone it or create a new array from its keys like shown in other answers from the link (foreach ($key in @($hash.Keys)) { ..) in order to update it while at the same time enumerating it or you would get the error:

Collection was modified; enumeration operation may not execute.

$mactable = @{
    'A' = '11:22:33:XX:YY:10'
    'B' = '11:22:33:XX:YY:20'
    'C' = '11:22:33:XX:YY:30'
    'D' = '11:22:33:XX:YY:40'
    'E' = '11:22:33:XX:YY:50'
}

foreach ($key in $mactable.Clone().Keys) {
    $value = $mactable[$key] -replace 'XX:YY', ($octet4hex + ':' + $octet5hex)
    $mactable[$key] = $value
}
$mactable['E'] # Outputs: 11:22:33:10:16:50
Sign up to request clarification or add additional context in comments.

2 Comments

You could also snapshot the hashtable keys into a new array to bypass the "modify while enumerating" issue - e.g. foreach( $key in @($mactable.Keys) ) { ... }. It's basically the same idea, but without cloning the entire hashtable in the process...
Thank you for this elaborate answer.I totally missed the fact to put the new value in a variable first and then add it to the mactable. That's why I focussed on the value part in my foreach loop. The ordered part was optional. So I opted for your latter solution and worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.