0

I have got 2 hashtable definitions, did some research and saw this helpful post

This highlights the differences within the hashtables as per the code below.

[hashtable] $template = @{

    "ID" = "1"
    "Firstname" = "2"
    "Surname" = "3"
    "Grade Score" = "4"
    "City" = "5"
    "School Name" = "6"
}


[hashtable] $data = @{

    "1" = "ID"
    "2" = "Firstname"
    "3" = "Surname"
    "4" = "Grade"
    "5" = "City"
    "6" = "School"
    "7" = "Comments"
    "8" = "UpdateDate"
}
#$data.GetEnumerator() | sort -Property Key
#$data | Get-Member
#$data


$template_count = $template.Count
$data_count = $data.count

if ($template_count -ne $data_count)
{
    write-output "Template count of $template_count columns does not match data column count of $data_count "
}
$template.GetEnumerator() | select key,@{ n='Value'; e={$data[$_.value]}}

I can see the output as in

      ID          | ID
      ------------|-----------
      School Name |    School
      Surname     |  Surname
      Grade Score |    Grade
      City        |     City
      Firstname   | Firstname

In this case, the values School Name and School are different, so is Grade Score and Grade. How can i highlight the differences ?

1
  • What do you mean by highlight the differences? Like 'Exact Match' = True / False or how? Commented Mar 11, 2021 at 19:44

1 Answer 1

1

Something like this? I'm not quite sure what you mean by "Highlight"

$expression={
    $re=[regex]$data[$_.Value]
    $match=$re.Match($_.Key).Value
    $_.Key -replace $match
}

$template.GetEnumerator() | select key,@{ n='Value'; e={$data[$_.value]}},@{n='Hightlight what does not Match?';e=$expression}


Key         Value     Hightlight what does not Match?
---         -----     -------------------------------
ID          ID                                       
School Name School     Name                          
Surname     Surname                                  
Grade Score Grade      Score                         
City        City                                     
Firstname   Firstname      
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, what I meant by highlight is say storing the columns that do not match in an array or custom object. Lastly on the data hash table, I would like to store the extra keys/values like comments and updateDate into the array or custom object.
The output of compare-object $($template.keys) $($data.Values) is what I would love, it shows differences in terms of Keys, whether name mismatch or new keys/values such as comments or updateDate. The issue with compare-objects is that I cannot easily process the output.

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.