Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

GetDoublesNuniqs

Publié le par damcuvelier

function GetDoubles {
    param($StrArr)
    $erroractionpreference = 'SilentlyContinue'
    $ici = (get-location).path
    if(!$here){$here = "C:\TEMP"}
    
    Set-Location $here

    $hashTable = @{}
    $StrArrs = ""

    foreach($itm in $StrArr.split(',')) {
        $itm2 = $itm.split('|')[1]
        if((($itm2.replace('/','').replace(':','')).trim()).length -gt 0) {
            $StrArrs += $itm2 + ','
        }
    }

    if($StrArrs) { $StrArrs = $StrArrs.trimend(',') }

    foreach($item in $StrArrs.split(',')) {
        $key = $item.trim()
        if($hashTable.containsKey($key)) {
            $hashTable[$key]++
        } else {
            $hashTable[$key] = 1
        }
    }

    $Duplicates = $hashTable.Keys | Where-Object { $hashTable[$_] -gt 1 }
    if(!$Duplicates) { $Duplicates = $null }

    Set-Location $ici
    return $Duplicates
}

function getuniqvals {
    param($ArrRefs)
    $erroractionpreference = 'SilentlyContinue'
    $ici = (get-location).path
    if(!$here){$here = "C:\TEMP"}
    
    Set-Location $here
    $doubleItems = GetDoubles -StrArr $ArrRefs
    $newArrRefs = $ArrRefs
    $results = ""

    if($doubleItems) {
        foreach($doubleItem in $doubleItems) {
            foreach($ArrRef in $ArrRefs.split(',')) {
                if($ArrRef -match $doubleItem) {
                    $newArrRefs = $newArrRefs.replace($ArrRef, '').replace(',,', ',').trimend(',').trimstart(',')
                }
            }
        }
    }

    if($newArrRefs.length -eq 0) {
        $results = $null
    } else {
        foreach($newArrRef in $newArrRefs.split(',')) {
            $result = $newArrRef.split('|')[1]
            $results += "$result,"
        }
    }

    if($results) { $results = $results.trimend(',') }
    Set-Location $ici
    return $results
}

function getuniqRefs {
    param($ArrRefs)
    $erroractionpreference = 'SilentlyContinue'
    $ici = (get-location).path
    if(!$here){$here = "C:\TEMP"}
    
    Set-Location $here
    $uniqRefs = ""
    $newArrRefs = $ArrRefs
    $uniqvals = getuniqvals -ArrRefs $ArrRefs

    foreach($uniqval in $uniqvals.split(',')) {
        foreach($newArrRef in $newArrRefs.split(',')) {
            $refnew = $newArrRef.split('|')[1]
            if($refnew -eq $uniqval) {
                $uniqRefs += $newArrRef.split('|')[0] + ','
            }
        }
    }

    if($uniqRefs.length -eq 0) {
        $uniqRefs = $null
    } else {
        $uniqRefs = $uniqRefs.trimend(',')
    }

    Set-Location $ici
    return $uniqRefs
}

# Données de test
$ref0 = '08/10/2024'
$ref1 = '07/10/2024'
$ref2 = '10/10/2024'
$ref3 = '10/10/2024'
$ref4 = '09/10/2024'
$ref5 = '07/10/2024'

$ArrRefs = "ref0|$ref0,ref1|$ref1,ref2|$ref2,ref3|$ref3,ref4|$ref4,ref5|$ref5"
$doubleItems = GetDoubles -StrArr $ArrRefs
$uniqvals = getuniqvals -ArrRefs $ArrRefs
$uniqRef = getuniqRefs -ArrRefs $ArrRefs

"doubleItems = $doubleItems`nuniqvals = $uniqvals`nuniqRef = $uniqRef"
# Résultats:
# $doubleItems = 07/10/2024 10/10/2024
# $uniqvals = 08/10/2024,09/10/2024
# $uniqRef = ref0,ref4

Commenter cet article