如何使用Powershell构建数组数组?



要对数据进行分组,每次有如下所示的"新人"时,我想将他们的信息添加到该临时数组中并将该数组重置为空。

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

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

$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"
}
}

Powershell没有提供使用基本数组创建数组数组的良好功能。

您可以使用哈希表数组或 PsCustomObject 为自己创建一个数组数组。

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

例如,我们创建 3 个数组,例如

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

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

$d = $a,$b,$c

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

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

最新更新