Consider the ff. lines of code:
Import-Csv $f | Select-Object -Property FName,GrossPay |
Foreach-Object -Process { $z = $_.FName.CompareTo(“”)
If ($z -NE 0) { … do a whole bunch of things }
}
The imported CSV file (which is small) is always fixed at 200 lines (i.e., records).
Each line/record is 100 bytes. But the typical actual data is just 30%, with the remaining 70% blank all the time.
In the above code, the comparison test in the variable ($z) signals the logical “end of file”, (an empty FName string)
but the processing continues until the last line/record.
Is there a PS syntax to break out of a script block – such as the one above – in order to save further unnecessary
processing of empty records? Another related question: is there a named constant variable similar to $TRUE, $FALSE
for checking an empty string? I tried using $NULL but failed.
Would be grateful for any advice, help or references on this issue.
Many thanks.
Thanks Mr Crompton for this wealth of information, offering numerous insights and giving me an increased understanding on how to use PS’s features. The second solution, to filter them early on at the CSV file stage, seems the smartest! Much thanks.