can you advise what should I search for in documentation. I do not know how to call what I am trying to do.
I have a $collection with $record and need to process each to $records a compare it. But I do not want to compare same records and repeat the comparison.
Dummy example of code:
foreach ($record in $collection) {
$start = $record.time
$label1 = $record.label
foreach ($otherrecord in $collection) {
$end = $otherrecord.time
$label2 = $otherrecord.label
$howlong = (New-TimeSpan -Start $start -End $end).totalhours
if ($howlong -gt 24) {
write-host "$label1 and $label2 with more than 24 hours time difference."
}
}
}
With the code above, it would compare:
A-A (do not want)
A-B
A-C
B-A (do not want)
B-B (do not want)
B-C
C-A (do not want)
C-B (do not want)
C-C (do not want)
It does not matter with 3 records, but I have collection with 80 000+ records. Is there a function or some trick for this? What should I look for?
You have two sources with matching labels? Normally you would want to do some kind of join operation on the other recordset (e.g. Label) and build an object:
While you can output a string or log, you are then reading it manually. If you use an object approach, you can see all of that data and can do comparisons and exports such as a csv.
To me it is simplest to use a Queue object. You can Dequeue() (remove) the first object during each run to ensure no repeats.
$queue = [System.Collections.Queue]::new($collection)
while ($queue.Count) {
$top = $queue.Dequeue()
$queue.ToArray() | Foreach-Object {
if (($top.Time - $_.Time).Duration().TotalSeconds -gt 86400) {
"{0} and {1} with more than 24 hours time difference" -f $top.label,$_.Label
}
}
}
This is not going to be an efficient process without have presorted data or using another tool built for bulk comparisons/joins.