gpt4 book ai didi

powershell - 在powershell中迭代 "pseudo infinite"哈希值

转载 作者:行者123 更新时间:2023-12-03 11:23:00 26 4
gpt4 key购买 nike

好吧,我的问题的标题可能有点含糊,因为我想在数组中“无限”循环,这与陷入无限循环不同......所以这就是我的意思:

考虑一下:

$notes=@{1='A';2='B';3='C';4='D';5='E';6='F';7=' G'};

我希望能够从上面的列表中选择任何项目,比如给我 1,3,5 (A,C,E),但如果我的选择超出了哈希的大小,那么我希望 powershell 能够计算找出如何移回表格的开头;例如,如果我说我想要第 5,7 和 9 项,那么那就是 E、G 和 B。

有什么简单的方法可以做到这一点吗?

非常感谢。

最佳答案

可能不是最理想的解决方案,但您始终可以使用 while 循环并减去 $notes 的计数,直到它小于计数:

$notes=@{1='A';2='B';3='C';4='D';5='E';6='F';7='G'};

$selection2 = 7, 8, 9, 10, 20, 21, 23

foreach ($item IN $selection2) {
while ($item -gt $notes.count ) {
$item = $item - $notes.count
}

Write-Output $notes[$item]
}

结果是:

G
A
B
C
F
G
B

或者,如果您的数组从 0 而不是 1 开始,则可以使用更简单的模数:

$notes=@{0='A';1='B';2='C';3='D';4='E';5='F';6='G'};

$selection2 = 5, 7 ,9

foreach ($item IN $selection2) {
Write-Output $notes[$item % $notes.count]
}

关于powershell - 在powershell中迭代 "pseudo infinite"哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59241329/

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