Question

Since Compare-Object cannot take wildcards, how would I compare two arrays for contents that make not be exactly the same in length or contents, but similar enough for a wildcard set or a regex to catch them.

Example:

$form = "First Name","Last Name","Address","Phone","Nickname","Fax Number"
$data = "First Name ","Last  Name","Street Address","Phone number","",Fax"

Can I compare the arrays without doing a bunch of if statements and operators? A switch would probably be more elegant, but I just so want to be able to do this on one line simlar to this:

Compare-Object ("*$form*) ("*$data*")

Instead of like this:

for($x=0;$x -lt $form.Count;$x++)
{
    [string]$xx = $x   
    if($form[$x] -like "*$data[$x]*" -or $data[$x] -like "*form[$x]*" -and $data[$x] -ne $null -and $form[$x] -ne $null)
   {
      $form[$x]
      $data[$x]
   }
   elseif($form[$x] -eq $null -and $data[$x] -eq $null)
   {
       "Form index $xx and Data index $xx are null"
   }
   elseif($form[$x] -eq $null)
   {
      "Form index $xx is null"
   }
   elseif($data[$x] -eq $null)
   {
       "Data index $xx is null"
   }
   else
   {
       "Data index $xx and Form index $xx are not even close to the same"
   }
}
Was it helpful?

Solution

I doubt you will be able to find something built-in that already has the "intelligence" you outlined in your sample code.

You could always make your own function or cmdlet to have your "one line" ability, allowing you to call it repeatedly. Or as an alternative you could write your own custom comparer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top