in Powershell 5
I do not understand, when debugging this code, why when the string " of the" is matched at the end of the main string, the method does not perform exit from the Class Method and continues to iterate.
Seems contra-intuitive to me.
Must I use the auxiliary variable and a label combined with break command?
something like here:
:JUMPASFOX foreach ($i in (1..10)) {
...{break JUMPASFOX}
I thought return $false anywhere causes exit. I cannot reconcile that my code does not work.
Class EndFinder {
[string[]]$Exclusions_End_Text=@(" of", " of the", " of a", "for a", "for the", " in", " the")
EndFinder(){}
[boolean]Contains_Exclusion_At_The_End ([string]$ClipText) {
#$found=$false
$this.Exclusions_End_Text |foreach {
$exclusion_length=$_.Length
$clip_length=$ClipText.Length
if ($exclusion_length -lt $clip_length) {
$tailing=$ClipText.Substring($clip_length-$exclusion_length,$exclusion_length)
if ($tailing -eq $_) {
#$found=$true
return $true
}
}
}
return $false
#return $found
}
}
$kb=[EndFinder]::New()
$kb.Contains_Exclusion_At_The_End("big problem of the")
... |foreach {}(which resolves to... |ForEach-Object {}) instead of a real loop -returnreturns control from the scriptblock to theForEach-Objectcommand, which then invokes the next iteration, oblivious to the "intuitive" behavior you're expecting.