Hi,
I am mad of how unpredictably break, continue and return behave in Powershell, namely for example if you have two nested loops. If two nested loops, then break breaks everything strange way.
However, today I have come across another unexplainable behaviour
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")
The purpose of this piece of code is to find whether the segment contains one of the possibilities named specifically in the array, at the end (not all the segment).
Note when debugging, when it comes to " of the" the I’d expect Method Contains_Exclusion_At_The_End shall return/end.
It does not.
Instead, it keeps iterating the loop.
I have to obfuscate the behaviour by using $found variable, but this does not avoid stupid iterating the items until the end.
I know they may be more ellegant way how to to it via calcualted property. But anyway. The return behaves oddly.
Do you know what mistake I do ? Or is powershell really so stupid language ?