如何使用Powershell构造数组的数组? [英] How to construct an array of arrays with Powershell?

查看:180
本文介绍了如何使用Powershell构造数组的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据,每当有一个如下所示的新人"时,我想在此添加他们的信息临时数组,然后将该数组重置为null.

To group data, every time there's a "new person" as below, I want to add their info to that temporary array and reset that array to null.

在将每个新人"数组设置为null之前,我想将该数组添加到人数组.数组的数组.

Before each "new person" array is set to null, I want to add that array to an array of people. An array of arrays.

如何将一个数组添加到另一个数组中?

How can I add one array into another?

$people = import-csv "./people.csv"

$h = @{}
$h.gettype()

$all_people

ForEach ($person in $people) {
  $new_person
  if ($person -match '[0-9]') {
    Write-host $person
  }
  else { 
    write-host "new person"
    write-host $person
  }
}

输出:

thufir@dur:~/flwor/people$ 
thufir@dur:~/flwor/people$ pwsh foo.ps1 

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Hashtable                                System.Object
new person
@{people=joe}
@{people=phone1}
@{people=phone2}
@{people=phone3}
new person
@{people=sue}
@{people=cell4}
@{people=home5}
new person
@{people=alice}
@{people=atrib6}
@{people=x7}
@{people=y9}
@{people=z10}

thufir@dur:~/flwor/people$ 

我有这样的东西:

$people = import-csv "./people.csv"

$all_people
$new_person = "new","person"

$new_person.GetType()

ForEach ($person in $people) {

  if ($person -match '[0-9]') {
    Write-host $person
    $new_person.Add($person)
  }
  else { 
    write-host "new person"
    write-host $person
    #$new_person = null
    $new_person = "new","person"
  }
}

推荐答案

是的,您可以创建数组数组.

Yes you can create array of arrays.

示例我们创建3个类似的数组

Example we create 3 arrays like

$a = 1..5
$b = 6..10
$c = 11..15

现在我们可以将它们添加到另一个数组$ d

Now we can add them in another array $d

$d = $a,$b,$c

现在,我们可以像这样访问它们:

Now we can access them like:

$d[0]
# Output 
# 1
# 2
# 3
# 4
# 5
$d[0][2]
# Output is $a arrays 3rd element

这篇关于如何使用Powershell构造数组的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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