gpt4 book ai didi

Powershell 哈希表问题

转载 作者:行者123 更新时间:2023-12-01 13:06:03 24 4
gpt4 key购买 nike

我正在编写一个脚本来获取一串字母并将它们转换为语音值。我遇到的问题是我无法引用哈希表中的值(请参阅下面的错误)。我不知道为什么,因为代码对我来说很好。

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

}

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]
}

最佳答案

每个 Char 都是一个丰富的对象,Change:

写主机 $alphabet[$char]



写主机 $alphabet["$char"]

或者

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

关于Powershell 哈希表问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3124871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com