Home:ALL Converter>Another simple way to compare array

Another simple way to compare array

Ask Time:2013-04-14T23:32:03         Author:Adeel ASIF

Json Formatter

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

Author:Adeel ASIF,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16000816/another-simple-way-to-compare-array
yy