A little known feature of the
#PowerShell switch statement is that it can process a file with the `-File` parameter and the `-Regex` parameter.
That is pretty fast to read and compare a 10K line text file.
@BrucePayette mentioned this when he was on my meetup talking about his new Braid language (sits on top of .NET and PS) and Pattern Matching.
----
$fileName = "$PSScriptRoot\random_text_10k.txt"
$countMap = [ordered]@{a = 0; b = 0; c = 0; d = 0; e = 0 }
$timing = Measure-Command {
switch -Regex -File $fileName {
'^a' { $countMap.a }
'^b' { $countMap.b }
'^c' { $countMap.c }
'^d' { $countMap.d }
'^e' { $countMap.e }
}
}
"Took {0} seconds to read a 10k line file and match" -f
$timing.TotalSeconds
$countMap
---- Result ----
Took 0.0584586 seconds to read a 10k line file and match
Name Value
---- -----
a 1944
b 2022
c 1993
d 2033
e 2008