简体   繁体   English

如何在PowerShell中使用[Array] :: FindIndex方法找到String数组元素的索引?

[英]How can I find the index of a String array element using the [Array]::FindIndex method in PowerShell?

another PowerShell noob question. 另一个PowerShell noob问题。

I have an array of strings that contains the names of businesses and I am writing a script that iterates through the folders of a directory and compares the folder names to the names contained in the array. 我有一个包含企业名称的字符串数组,并且我正在编写一个脚本,该脚本遍历目录的文件夹并将文件夹名称与数组中包含的名称进行比较。 When I find a match I need to get the array index where the match was found. 找到匹配项时,我需要获取找到匹配项的数组索引。 I've done this successfully for exact matches using the following code: 我已经使用以下代码成功完成了针对完全匹配的操作:

foreach($file in Get-ChildItem $targetDirectory)
{
    if($businesses -like "$file*")
    {
        $myIndex = [array]::IndexOf($businesses, [string]$file)
    }
}

The reason I use -like "$file*" is because sometimes the name of the business has "LLC" or "INC" or something like that, which occasionally wasn't included when we named the folders(and vice versa). 我之所以使用-like "$file*"的原因是,有时企业名称使用“ LLC”或“ INC”或类似名称,在我们为文件夹命名时有时不包括(反之亦然)。 I would like to modify my $myIndex line to find these indexes as well. 我想修改我的$myIndex行以找到这些索引。

I'm trying to use 我正在尝试使用

 $myIndex = [array]::FindIndex($Merchant, {args[0] -like "$file*"})

But I'm getting the error 但是我得到了错误

Cannot find an overload for "FindIndex" and the argument count: "2". 找不到“ FindIndex”和参数计数:“ 2”的重载。

I've tried casting the second argument using [Predicate[string]]{args[0] -like "$file*"} but get the same result. 我尝试使用[Predicate[string]]{args[0] -like "$file*"}强制转换第二个参数,但是得到相同的结果。 I've read the documentation for the method but one of my basic problems is I don't understand the System.Predicate type. 我已经阅读了该方法的文档,但是我的基本问题之一是我不了解System.Predicate类型。 I'm not even sure if I'm writing it properly; 我什至不确定我是否编写正确; I found an example that was similar but used [int] instead. 我发现了一个类似的示例,但改用了[int] Maybe I'm going about it all wrong but I feel like I'm close and just can't find the missing piece of the puzzle anywhere. 也许我正在解决所有错误,但是我感觉自己已经接近,无法在任何地方找到谜题中缺失的部分。

Thanks in advance for any help. 在此先感谢您的帮助。

[Array]::FindIndex($Merchant, [Predicate[string]]{}) only works if $Merchant is of type string[] : [Array]::FindIndex($Merchant, [Predicate[string]]{})仅在$Merchant类型为string[]

$Merchant is a string array, works: $Merchant是一个字符串数组,可以工作:

PS C:\> $Merchant = 1,2,3 -as [string[]]
PS C:\> [array]::FindIndex($Merchant,[Predicate[String]]{param($s)$s -eq "3"})
2

$Merchant is an array of Int32 s, doesn't work: $MerchantInt32的数组,不起作用:

PS C:\> $Merchant = 1,2,3 -as [int[]]
PS C:\> [array]::FindIndex($Merchant,[Predicate[String]]{param($s)$s -eq "3"})
Cannot find an overload for "FindIndex" and the argument count: "2".
At line:1 char:1
+ [array]::FindIndex($Merchant,[Predicate[String]]{param($s)$s -eq "3"})
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Works fine when cast to Predicate[int] : 强制转换为Predicate[int]时工作正常:

PS C:\> [array]::FindIndex($Merchant,[Predicate[int]]{param($s)$s -eq 3})
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在适当的时间间隔内使用方法 findIndex() 找到数组中的最后一个索引? JS - How can I find last index in array with method findIndex() in proper interval? JS 如何使用Powershell通过值查找数组中元素的索引 - How to find the index of an element in an array by its value using Powershell 使用 findIndex 数组方法时出现意外结果 - unexpected result while using findIndex array method 如何使用方法返回数组元素的索引? - How can I use a method to return the index of an array element? 如何找到数组中第二大元素的索引? - How can I find the index of the second greatest element in my array? 如何在数组数组中找到元素的索引? - How can I find the index of an element, in an array of arrays? 我可以在 FindIndex 期间将数组中的 object 字符串化吗? - Can I stringify an object in an array during FindIndex? 使用Powershell,我如何计算数组中每个元素的出现? - Using Powershell, how can i count the occurrence of each element in an array? 为什么 splice 和/或 findIndex 不允许更改数组/索引 0 的第一个元素? - Why does splice and/or findIndex not allow a change to the first element of an array/index 0? 如何使用MongoDB 3.0按索引访问数组元素? - How can I access array element by index using MongoDB 3.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM