I'm trying to iterate over each key of a hash map, and for each key, look it's value up in 3 different maps, assigning to a psCustomObject. For some reason, after just going through the foreach once, the resultsCase looks like this, and the length is the same as the map I got the Keys out of in the foreach statement. Every time it iterates thru the foreach, it populates the 69 rows again, over-writing them:
Object[69]
[0]: @{sdkErr=HopResultMap3[seFatal];sdkDesc=HopResultMap2[seFatal];sdkOutErr=HOP_FATAL_ERROR}
[1]:@{sdkErr=HopResultMap3[seInterface];sdkDesc=HopResultMap2[seInterface];sdkOutErr=HOP_INTERFACE}
...
[68]: @{...}
As you can see, it's only resolving the map lookup for the last item above.
I'm not sure why it's not doing the lookup in the other map, and actually putting the value in the $resultCase. It only does it right for the last one, which is coincidentally, the map I'm using for the foreach. I designed this so I could use the key from the first map and use it to look up values in the other two maps. I need a combined map here so I can re-use code for something that has a different file structure.
$resultCase = foreach ($key in $HopResultMap.Keys){
[PSCustomObject][ordered]@{
sdkErr = $($HopResultMap3[$key])
sdkDesc = $($HopResultMap2[$key])
sdkOutErr = $($HopResultMap["$($key)"])
}
}
I've also tried this with this sort of thing for Map2/3 and it didn't change the results:
$($HopResultMap2[$($key)])
and also
$($HopResultMap2["$($key")])
For reference, the maps look like this:
HopResultMap:
OrderedDictionary[69]
[0]:@{seFatal,Hop_Fatal_Error}
[1]:@{seInterface,Hop_Interface}
...
HopResultMap2:
OrderedDictionary[69]
[0]:@{seFatal,"Fatal error"}
[1]:@{seInterface,"Interface not.."}
...
HopResultMap3 (have a little extra in the third one, but the extras have a different key...like sc...):
OrderedDictionary[120]
...
[16]:@{seFatal,"0"}
[17]:@{seInterface,"1"}
...
I can't really change the psCustomObject data structure, because I have another model that has the same data structure. This is with powershell 5.1, and VSCode.
Update:
The 3 maps are all constructed (similar to) this way:
function Get-Contents60{
[cmdletbinding()]
Param ([string]$fileContent)
#m_HopErrorMap.insert(make_pair(
#MAKEWORD(scError,sePaperJam),
#HOP_JAM ));
# create an ordered hashtable to store the results
$errorMap = [ordered]@{}
# process the lines one-by-one
switch -Regex ($fileContent -split '\r?\n') {
'MAKEWORD\([^,]+,([^)]+)\),' { # seJam, seFatal etc.
$key = $matches[1]
}
'(HOP_[^)]+)' {
$errorMap[$key] = $matches[1].Trim()
}
}
return $errorMap
}