简体   繁体   English

搜索多数组并返回索引

[英]Search an multi array and return index

I have this array: 我有这个数组:

$Cluster = @( ("vcdvc012", "CL-CC5-DR"),
    ("vcdvc013", "CL-CCL3-DR"))

Now I would like to search for "vcdvc012" to be able to get as result "CL-CC5-DR". 现在,我想搜索“ vcdvc012”以得到结果“ CL-CC5-DR”。

What is the best way to do this? 做这个的最好方式是什么? Gabrie 加布里

尝试:

$Cluster | ? { $_[0] -eq 'vcdvc012' } | % { $_[1] }

Frode has a general answer but it relies on knowing the location of the element. Frode有一个一般性的答案,但是它依赖于知道元素的位置。 Since you are dealing with arrays the operators -contains and -in would be beneficial here. 由于您要处理数组,因此操作符-contains-in会很有用。 Won't change much for the selection of the second element unless you created your own custom objects. 除非您创建了自己的自定义对象,否则第二个元素的选择不会有太大变化。

$Cluster | Where-Object {$_ -contains 'vcdvc012'} | ForEach-Object{ $_[1] }
$Cluster | Where-Object {'vcdvc012' -in $_} | ForEach-Object{ $_[1] }

Both would work just the same. 两者的工作原理相同。 The latter is more intuitive as many confused the functionality of -contains . 后者更直观,因为许多人混淆了-contains的功能。 -in was made available in v3.0 -in在v3.0中可用

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM