I am trying to find a better way to edit a list of hashtables in powershell. I have successfully looped though the list to find duplicates, but I am not sure how to edit the hashtable and update the list. Does anyone know how to do this at scale?
The test data is formated like this : ( I know it is terrible, but that is the way of the world! )
$TEST_DATA = @(
"CA>TORONTO>MAP-LAYOUT-1", `
"US>NEW-YORK>MAP-LAYOUT-25", `
"US>DENVER>MAP-LAYOUT-2", `
"UK>LODNON>MAP-LAYOUT-2", `
"EU>PARIS>MAP-LAYOUT-25", `
"EU>VENICE>MAP-LAYOUT-2", `
"EU>BERLIN>MAP-LAYOUT-8", `
"EU>PARIS>MAP-LAYOUT-30", `
"EU>VENICE>NEW-MAP-LAYOUT-2", `
"EU>ATHENS>NEW-MAP-LAYOUT-8", `
"CA>TORONTO>NEW-MAP-LAYOUT-1", `
"CA>VANCOUVER>MAP-LAYOUT-1", `
"US>MIAMI>MAP-LAYOUT-25", `
"RU>MOSCOW>MAP-LAYOUT-2", `
"RU>ST-PETERSBURG>MAP-LAYOUT-2", `
"JP>TOKYO>MAP-LAYOUT-25", `
"EU>PARIS>MAP-LAYOUT-100", `
"AU>SYDNEY>MAP-LAYOUT-3", `
"AU>PERTH>MAP-LAYOUT-3", `
"MX>MEXICO-CITY>MAP-LAYOUT-5", `
"MX>TIJAUNA>MAP-LAYOUT-1", `
"CN>SHANGHAI>MAP-LAYOUT-8", `
"CA>CALGARY>MAP-LAYOUT-1"
)
and my little powershell script is like this :
$MAPS = @()
$TEST_DATA | Foreach {
$COUNTRY = ($_).split('>')[0]
$CITY = ($_).split('>')[1]
$MAP = ($_).split('>')[2]
If ( $MAPS.count -gt 0) {
$MAPS | Foreach {
$MAPS_ROW = $_
Write-Host $MAPS_ROW
If (( $MAPS_ROW.COUNTRY -eq $COUNTRY ) -and ( $MAPS_ROW.CITY -eq $CITY )) {
Write-Host '--------' -ForegroundColor Red
Write-Host "CONFLICT DETECTED ! "
write-Host '--------' -ForegroundColor Red
Write-Host ""
Write-Host "CONFLICT MAP : $MAPS_ROW"
Write-Host ""
If (($MAP.lenght -gt $MAPS_ROW.MAP.length) -or ( $MAP.lenght -gt ($MAPS_ROW.MAP.trim("NEW-")).length)) {
$MAPS.Remove($MAPS_ROW)
$NEW_MAP = New-Object PSObject -property @{ COUNTRY="$COUNTRY"; CITY="$CITY"; MAP="$MAP"; }
$MAPS += $NEW_MAP
} else {
Write-Host '--------' -ForegroundColor Yellow
Write-Host "FALSE MAP CONFLICT DETECHED ! $MAPS_ROW "
Write-Host '--------' -ForegroundColor Yellow
}
} else {
Write-Host '--------' -ForegroundColor BLue
Write-Host "NOT IN : 'MAPS'"
Write-Host '--------' -ForegroundColor Blue
}
}
$NEW_MAP = New-Object PSObject -property @{ COUNTRY="$COUNTRY"; CITY="$CITY"; MAP="$MAP"; }
$MAPS += $NEW_MAP
} else {
Write-Host '--------' -ForegroundColor BLue
Write-Host "First MAP Entry"
Write-Host '--------' -ForegroundColor Blue
$NEW_MAP = New-Object PSObject -property @{ COUNTRY="$COUNTRY"; CITY="$CITY"; MAP="$MAP"; }
$MAPS += $NEW_MAP
}
}
$SORTED_MAPS = $MAPS | Sort-Object { $_.COUNTRY }, { $_.CITY }, { $_.MAP } | Select-Object @{n="COUNTRY";e={$_.COUNTRY}}, @{n="CITY";e={$_.CITY}}, @{n="MAP";e={$_.MAP}}
Write-Host '--------' -ForegroundColor Blue
$SORTED_MAPS
Write-Host '--------' -ForegroundColor Blue
and the looping sort of works and I get a nice clean sorted list at the end.
COUNTRY CITY MAP
------- ---- ---
AU PERTH MAP-LAYOUT-3
AU SYDNEY MAP-LAYOUT-3
CA CALGARY MAP-LAYOUT-1
CA TORONTO MAP-LAYOUT-1
CA TORONTO NEW-MAP-LAYOUT-1
CA VANCOUVER MAP-LAYOUT-1
CN SHANGHAI MAP-LAYOUT-8
EU ATHENS NEW-MAP-LAYOUT-8
EU BERLIN MAP-LAYOUT-8
EU PARIS MAP-LAYOUT-100
EU PARIS MAP-LAYOUT-25
EU PARIS MAP-LAYOUT-30
EU VENICE MAP-LAYOUT-2
EU VENICE NEW-MAP-LAYOUT-2
JP TOKYO MAP-LAYOUT-25
MX MEXICO-CITY MAP-LAYOUT-5
MX TIJAUNA MAP-LAYOUT-1
RU MOSCOW MAP-LAYOUT-2
RU ST-PETERSBURG MAP-LAYOUT-2
UK LODNON MAP-LAYOUT-2
US DENVER MAP-LAYOUT-2
US MIAMI MAP-LAYOUT-25
US NEW-YORK MAP-LAYOUT-25
and my problems are :
- How to remove the duplicates from the list?
I tried to sort with the unique switch but coudn't get it to work properly...
- How to make sure that ther is one unique entry for each city with the most correct map name?
I tried to do a check on country and city fields, but that doesn't seem to allow me to add and remove the hashtable from the array.
- Is it better to just create a new arraylist and try to to sort the map bit ?
with conditions like .
If the map contains "new-" or some other tag it has a highest preference. eg: "EU VENICE NEW-MAP-LAYOUT-2 " would be the result for venice.
If the has the highest increment value would be the second highest priority... eg: "EU PARIS MAP-LAYOUT-100 " would be the result for paris.
Thanks in advance for any advice on how to tackle these issues...
Ta,
X