我有一个使用 foreach 关键字的循环:
foreach ($ln in Get-Content "c:\ATextFile.txt" )
是否可以在迭代过程中找出 $ln 引用的数组索引?或者我是否需要为每次循环迭代创建和增加一个单独的计数变量?
最佳答案
是的,要么使用 for 循环(类似于 (gc c:\ATextFile.txt).count
将是上限)或外部计数器。
相关答案(对于 C#,但基本上都使用相同的可枚举概念):How do you get the index of the current iteration of a foreach loop?
关于powershell - 在 Powershell foreach 循环中检索数组索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7183078/