基于过滤第二阵列中的阵列? [英] Filtering an array based on a second array?

查看:132
本文介绍了基于过滤第二阵列中的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据数组:

$arrnames=('a','b','c')
$arrtypes=('x','y','z')

和我有一个查找数组:

$arrlookup=('y','z')

我需要在返回的元素 $ arrnames 其中$ arrtypes匹配的元素包含在 $ arrlookup 。我一直在玩的foreach 遍历 $ arrnames 看着相同的元素$ arrtypes 通过索引,但它变得马虎,我知道必须有一个更好的办法。

I need to return the elements in $arrnames where the matching element in $arrtypes is contained in $arrlookup. I've been playing with foreach looping over $arrnames and looking at the same element in $arrtypes by index but it's getting sloppy and I know there has to be a better way.

我已经看到了一些类似的问题,但不是任何令我专门回答了这个问题。作为一个PowerShell非常新手,我仍然在学习如何例子适应我的需要。任何帮助是极大的AP preciated。

I've seen some similar questions, but not anything that strikes me as specifically answering this problem. Being a Powershell very newbie I'm still learning how to adapt examples to my needs. Any help is greatly appreciated.

我的code(用于应用到上面的例子技术 - 实际code是用从XML文件导出复杂阵列,并有很多的调试code,所以复制/粘贴是不切实际的)

My code (technique used as applied to above example - actual code is using complex arrays derived from an XML file and has a lot of debugging code so copy/paste is impractical)

foreach ($a in $arrnames) {
    $t=$arrtypes[$arrnames.IndexOf($a)]
    if ($arrlookup.Contains($t)) {
       $arrresult+=$a
    }
)

$ arrresult应该包含有一个类型(从$ arrtypes),它是在$ arrlookup $ arrnames的成员。

$arrresult should contain the members of $arrnames that have a type (from $arrtypes) that is in $arrlookup.

有没有使用对象方法,过滤和管道简单地提取元素,而foreach循环的方式

Is there a way to use object methods, filtering and the pipeline to simply extract the elements without a foreach loop

编辑 - 这里是创建实际的阵列实际code - $建立在XML文档:

Edit - here's the actual code that creates the actual arrays - $builds is an XML document:

$names=$builds.builds.project.name
$types=$builds.builds.project.type

查找表是已知的:

The lookup table is known:

$FXCopTypes=@('batch','component','web')

XML文件我也有控制权,但我看不出有任何的方式来简化它比实施哈希表code,但与上面的数组。

The XML file I also have control over, but I don't see any way to simplify it more than implementing the hash table code but with the above arrays.

推荐答案

我想你需要改变你输入序,能够在这里做什么不同。你所要求的是 $ arrnames $ arrtypes ,真正成为一个哈希表。这样,你可以使用键访问值。

I think you need to change you input inorder to be able to do anything different here. What you are asking for is $arrnames and $arrtypes to really be a hashtable. That way you can access the values using keys.

既然这样,我会做这个创建哈希表。第二个循环展示了如何返回每个匹配的值。

As it stands I would do this to create the hashtable. The second loop shows how to return each matching value.

$hash = @{} 

for($index = 0; $index -lt $arrtypes.Count; $index++){
    $hash.($arrtypes[$index]) =  $arrnames[$index]      
}

$arrresult = $arrlookup | ForEach-Object{
    $hash[$_]
}

这将返回

b
c

如果你可以让你的输入来创建哈希表则减少了需要重建。此外,如果你知道前手的查找以及你就可以过滤它,只是有你想要的输出。

If you could get your input to create that hash table then it reduces the need to rebuild it. Also if you know the lookup before hand as well you can filter it then and just have the output you want.

这篇关于基于过滤第二阵列中的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆