0

I want to compare two arrays, I want to know if there is an another way to do this more simply than I did?

This is my code:

$array1 = @()
$array1 += "TEST1 LAPTOP DSGF65"
$array1 += "TEST2 LAPTOP DJDJD"
$array1 += "TEST3 LAPTOP DJDJD"
$array1 += "TEST4 LAPTOP DJDJD"

$array2 = @()
$array2 += "xxxxx"
$array2 += "test3"
$array2 += "xxxxx"
$array2 += "xxxxx"
$array2 += "test1"
$array2 += "xxxxx"
$array2 += "xxxxx"
$array2 += "test2"
$array2 += "test4"

$z = 0

for ($i = 0; $i -lt $array2.count; $i++)

{

  for ($j = 0; $j -lt $array1.count; $j++)

  {

    if ($array1[$j].Substring(0,5).ToLower() -eq $array2[$i])
    {
      $z++
    }
  }

}

Write-Host $z "elements"

The write-Host $z return 4

Thanks for your suggestions

1 Answer 1

1

I'd suggest:

$z = 0
$array2 |
 foreach {$z += ($array1 -match $_).count}

 $z
Sign up to request clarification or add additional context in comments.

Comments

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.