Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

foreach ($key in $map.keys) not behaving as expected

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$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
}

>Solution :

Since you are dealing with arrays of hash tables and not with hash tables, referencing by [$key] will be null, this should likely solve the problem:

$dict1 = @(
    @{seFatal = 'Hop_Fatal_Error'}
    @{seInterface = 'Hop_Interface'}
)

$dict2 = @(
    @{seFatal = "Fatal error"}
    @{seInterface = "Interface not.."}
)

$dict3 = @(
    @{seFatal = "0"}
    @{seInterface = "1"}
)

foreach($key in $dict1.Keys)
{
    [pscustomobject]@{
        sdkErr = $dict3.$key
        sdkDesc = $dict2.$key
        sdkOutErr = $dict1.$key
    }
}

The result would be:

sdkErr sdkDesc         sdkOutErr
------ -------         ---------
0      Fatal error     Hop_Fatal_Error
1      Interface not.. Hop_Interface
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading