Reputation: 39
I'm trying to get pull a value from an array based on the index and I can't get it to work for some reason. Keeps returning -1
$alphabet = [char[]]([char]'a'..[char]'z')
$AZ = 'A'..'Z
##Option 1
[array]::indexof($AZ, 2)
##Option 2
$AZ.IndexOf( 2 )
both of these options return -1
I should say I tried following this article. https://devblogs.microsoft.com/scripting/find-the-index-number-of-a-value-in-a-powershell-array/
Upvotes: 1
Views: 143
Reputation: 60045
Hope this helps you identify the problem:
PS /> [char[]]$alphabet = 'a'..'z'
PS /> [array]::IndexOf($alphabet,'f') #=> Returns -1
PS /> [array]::IndexOf($alphabet,[char]'f') #=> Returns 5
PS /> $alphabet.IndexOf('f') #=> Returns -1
PS /> $alphabet.IndexOf([char]'f') #=> Returns 5
PS /> ([char]'f').Equals([string]'f') #=> Returns false, here is your hint
Upvotes: 3