If you know that the value occurs only once in the array, the [array]::IndexOf() method is a pretty good way to go:
$array = 'A','B','C'
$item = 'B'
$ndx = [array]::IndexOf($array, $item)
Besides being terse and to the point, if the array is very large the performance of this approach is quite a bit better than using a PowerShell cmdlet like Where-Object. Still, it will only find the first occurrence of the specified item. But you can use the other overload of IndexOf to find the next occurrence:
$ndx = [array]::IndexOf($array, $item, $ndx+1)
$ndx will be -1 if the item isn't found.
Answer from Keith Hill on Stack OverflowIf you know that the value occurs only once in the array, the [array]::IndexOf() method is a pretty good way to go:
$array = 'A','B','C'
$item = 'B'
$ndx = [array]::IndexOf($array, $item)
Besides being terse and to the point, if the array is very large the performance of this approach is quite a bit better than using a PowerShell cmdlet like Where-Object. Still, it will only find the first occurrence of the specified item. But you can use the other overload of IndexOf to find the next occurrence:
$ndx = [array]::IndexOf($array, $item, $ndx+1)
$ndx will be -1 if the item isn't found.
Use a for loop (or a foreach loop that iterates over the array index...same difference). I don't know of any system variable that holds the current array index inside a foreach loop, and I don't think one exists.
# example array
$array = "A", "B", "C"
$item = "B"
0..($array.Count - 1) | Where { $array[
item }
powerpoint - Need to add and find element index value in arraylist in powershell? - Stack Overflow
How to find the indexOf element in ArrayList in PowerShell - Stack Overflow
Powershell find value in arraylist - Stack Overflow
How to find the index of an item in an array?
try Something like this:
Copy$array1=import-csv "C:\temp\log.csv"
$array2=import-csv "C:\temp\log2.csv"
#modify founded and output not founded
$toadd=$array2 | %{
$current=$_
$founded=$array1 | where pc -eq $current.pc | %{$_.date=$current.date;$_}
if ($founded -eq $null)
{
$current.city='UNKNOW'
$current
}
}
#output of $array1 modified and elements to add
$array1, $toadd
Here is a sample I created that might help. Note: I use List types instead of ArrayList ones. Also, it assumes only one possible matching PC name in the data to be updated. You'll have to alter it to update the file since it merely updates the first List variable. Let me know how it goes.
Copy๏ปฟ[PSCustomObject]
{
[string] $pc,
[string] $name,
[string] $date,
[string] $city
}
[System.Collections.Generic.List[PSCustomObject]] $list1 = Import-Csv "C:\SOSamples\log.csv";
[System.Collections.Generic.List[PSCustomObject]] $list2 = Import-Csv "C:\SOSamples\log2.csv";
[PSCustomObject] $record = $null;
[PSCustomObject] $match = $null;
foreach($record in $list2)
{
# NOTE: This only retrieves the FIRST MATCHING item using a CASE-INSENSITIVE comparison
$match = $list1 | Where { $_.pc.ToLower() -eq $record.pc.ToLower() } | Select -First 1;
if($match -eq $null)
{
Write-Host "Not Found!";
$list1.Add($record);
}
else
{
Write-Host "Found!";
$match.date = $record.date;
}
}
Write-Host "--------------------------------------------------------------------"
foreach($record in $list1)
{
Write-Host $record
}
For example i want to print a row from a table that is made out of 3 arrays by the value of an item in one of the arrays(name btw)