I don't often ask questions (most times the problem can be solved by some research right?) but I just wanna hear out your opinion as probably there is a better (more efficient way of doing this).
So let's see, the code below works perfectly fine and it serves it's purpose. The result of the code is a hashmap of hashmaps that I need as lookup table for on another work.
Background:
$ccDbis an array consisting of around 200k items, the properties arecompanyCd, costCenterNbr, costCenterShortNm, costCenterLongDescr.- Each property has to be trimmed (please don't ask me to trim my Db, sadly I can't).
costCenterNbris contained oncompanyCd, meaning, eachcompanyCdcan have multiplecostCenterNbr.companyCdcan contain X amount ofcostCenterNbr.costCenterNbrhave unique value, same forcompanyCd.costCenterShortNmandcostCenterLongDescrare correlated withcostCenterNbr
The Issue:
This map has to be constructed on each run of my script because the information is taken from SQL tables (which changes all the time). Building this map takes around 15 minutes (on a pretty good server, 2CPUs 12Cores).
The question:
Do you see a way this code could be improved for a faster / more efficient execution?
$ccMap=@{}
foreach($line in $ccDb)
{
$companyCd=$line.companyCd.trim()
$costCenterNbr=$line.costCenterNbr.trim()
$costCenterShortNm=$line.CostCenterShortNm.trim()
$costCenterLongDescr=$line.CostCenterLongDescr.trim()
$coceMap=@{
$costCenterNbr=@{
shortDesc=$costCenterShortNm
longDesc=$costCenterLongDescr
}
}
if($ccMap.ContainsKey($companyCd))
{
$ccMap[$companyCd]+=$coceMap
}
else
{
$ccMap.Add($companyCd,$coceMap)
}
}
I'm sorry for the long explanation, but I feel like it's better to give the most information up front. Any help is very much appreciated. In addition, I understand PowerShell is a pretty awful language for what I'm doing and C# would be probably a lot more efficient but it is what it is.
Edit: Adding measurements for reference.
Edit:
Thanks a lot @Mathias R. Jessen, here are the measure results of his code. Excellent code.


