arrays - 在两个数组之间匹配值并排列在新数组中

标签 arrays powershell

所以我有2个数组

数组1

4
5
6
7
8
9
10
11

array2(aa 5)是数组中的一个元素,依此类推
aa  5
bb  4
cc  6
dd  8
ee  9
ff  10
gg  7
hh  11

我想做的是在array2中排列字母以匹配第一数组中的数字,并在一个新数组中获取它,因此输出将像这样。

数组3
bb
aa
cc
gg
dd
ee
ff
hh

 $array1 | %{

       if ($array2 -match $_) {

        array1 | select-string -pattern '(\w\w)'| % {$_.Matches.groups[1].value}


        }
    }


最佳答案

以下是使用哈希表的解决方案。

$array1 = 4,5,6,7,8,9,10,11

$array2 = @"
aa  5
bb  4
cc  6
dd  8
ee  9
ff  10
gg  7
hh  11
"@ -split [Environment]::NewLine

# create hashtable
$h = @{}
$array2 | foreach { $val, $key = $_ -split "\s+"; $h.Add([int]$key, $val) }

$array3 = $h[$array1]

关于arrays - 在两个数组之间匹配值并排列在新数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60591624/

相关文章:

c# - Powershell 加载自定义程序集配置文件

php - 了解数组排序行为 php

c++ - 指针值改变C++

java - 返回方法时出现 ArrayExceptionOutOfBounds

powershell - 在脚本 block 的父范围内更改局部变量

powershell - 如果包含 “*”的代理创建绕过列表失败

javascript - 如何将数字从预定义集合转换为加数数组?

javascript - 如何用另一个给定的数组元素替换数组元素?

C# 应用程序调用 Powershell 脚本问题

powershell - 使用 powershell 列出 IIS 中的 Web 应用程序