Powershell哈希表问题

Sup*_*oad 3 powershell

我正在编写一个脚本,以接收一串字母并将其转换为语音值。我遇到的问题是我无法引用哈希表中的值(请参见下面的错误)。我不确定为什么代码看起来对我来说很好。

Index operation failed; the array index evaluated to null.
At C:\Scripts\test.ps1:8 char:23
    + write-host $alphabet[ <<<< $char]
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArrayIndex

}
Run Code Online (Sandbox Code Playgroud)

param($ string = $ {throw'输入一个字符串'))

$alphabet = @{
"A" = "Alfa";
"B" = "Bravo ";
"C" = "Charlie ";
"D" = "Delta ";
"E" = "Echo ";
"F" = "Foxtrot ";
"G" = "Golf ";
"H" = "Hotel ";
"I" = "India ";
"J" = "Juliett";
"K" = "Kilo ";
"L" = "Lima ";
"M" = "Mike ";
"N" = "November ";
"O" = "Oscar ";
"P" = "Papa ";
"Q" = "Quebec ";
"R" = "Romeo ";
"S" = "Sierra ";
"T" = "Tango ";
"U" = "Uniform ";
"V" = "Victor ";
"W" = "Whiskey ";
"X" = "X-ray";
"Y" = "Yankee ";
"Z" = "Zulu ";
}

clear-host
$charArray = $string.ToCharArray()
foreach ($char in $charArray)
{
    write-host $alphabet[$char]
}
Run Code Online (Sandbox Code Playgroud)

Sha*_*evy 5

每个字符都是一个丰富的对象,请更改:

写主机$ alphabet [$ char]

写主机$ alphabet [“ $ char”]

要么

写主机$ alphabet [$ char.ToString()]