在Powershell中将元素添加到数组

乔治

我正在尝试将Totals行添加到数组$response_table例如,该行应为Totals = 56(数字)。我试过了$response_table += @{Name="Total"; Count=55};,它添加了一条包含垃圾数据的行。您能帮忙添加吗?代码如下

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$current_month = (Get-Date).month;
$current_year = (Get-Date).year;

$response_table=@();
$what_year=2019
$month_count=12
$current_month, $total_year, $current_date_interval;
$total_monthly=@(); # empty array, will be populated with values from the API
#$numbers += 5, 2, 3; # to add values to array

if ($what_year -eq $current_year)
{
    $month_count = $current_month-1;
}

for ($current_month=1; $current_month -le $month_count; $current_month++)
{
    $current_date_interval = -join($what_year, "-", $current_month);
    $uri="https://data.police.uk/api/crimes-street/bicycle-theft?poly=53.950624,-1.059234:53.951301,-1.049181:53.947361,-1.043420:53.950333,-1.030671:53.952997,-1.016427:53.950189,-1.013653:53.929487,-1.042286:53.942512,-1.054948:53.941936,-1.057172:53.944481,-1.060155s8&date="+$current_date_interval;
    $response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers
    $response_table += $response | Group month |Select -Property name,count
    Write-Host $response_table;  
    $response | Group month |Select -Property name,count
    $total_year += $response.count;
}
Write-Host ($response_table|Measure-object -Property count -sum)
$response_table += @{Name="Total"; Count=55};
# does not work
$response_table | export-csv "list.csv" -notypeinformation
#add-content "list.csv" "Total,$total_year"
Write-Host "Yearly total"$total_year;
mklement0

正如AdminOfThings指出的那样,您错误地将哈希表添加到现有的自定义对象数组Select-Object输出)中。

要在PSv3 +中构造自定义对象,只需将[pscustomobject]其放在哈希表文字之前,这对您而言意味着:

$response_table += [pscustomobject] @{Name="Total"; Count=55}

现在您的Export-Csv命令应该可以正常工作了。

顺便说一句:在PowerShell 6.x之前,Export-Csv仅(有意义地)支持将自定义对象作为输入,不支持hashtables作为输入v7添加了对哈希表的支持。


通常避免使用+=追加到数组:

数组是固定大小的数据结构,因此,当您“附加到”数组时,PowerShell必须执行的操作+=每次在幕后创建一个数组,这在循环中效率很低。

在使用高效的就地可扩展数据结构(例如[System.Collections.ArrayList][System.Collections.Generic.List[object]])时,最简单,最快的方法是通过将整个循环分配为表达式,PowerShell轻松[object[]]为您收集数组(中的多个输出对象。变量

[array] $response_table = for ($current_month=1; $current_month -le $month_count; $current_month++)
  {
    $current_date_interval = -join($what_year, "-", $current_month);
    $uri="https://data.police.uk/api/crimes-street/bicycle-theft?poly=53.950624,-1.059234:53.951301,-1.049181:53.947361,-1.043420:53.950333,-1.030671:53.952997,-1.016427:53.950189,-1.013653:53.929487,-1.042286:53.942512,-1.054948:53.941936,-1.057172:53.944481,-1.060155s8&date="+$current_date_interval;
    $response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers

    # *Output* the result of this pipeline, and PowerShell will
    # collect all outputs for you
    $response | Group month | Select -Property name,count

    $total_year += $response.count;
  }

# It's fine to use += to add the total, because you're only using it *once*.
$response_table += [pscustomobject] @{Name="Total"; Count=55}

注意:即使循环恰好只有1次迭代[array]类型约束也可确保该$response_table数组成为数组

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在PHP中将元素添加到数组

来自分类Dev

在Swift中将元素添加到可选数组

来自分类Dev

如何在php中将元素添加到子数组

来自分类Dev

无法从SQL查询中将获取的元素添加到数组

来自分类Dev

在TCL中将元素添加到字典中的数组

来自分类Dev

用于将元素添加到数组的PowerShell函数

来自分类Dev

在Scala中将元素添加到列表

来自分类Dev

在Java中将元素添加到Iterable

来自分类Dev

在Flutter中将ID添加到元素

来自分类Dev

在Scala中将元素添加到列表

来自分类Dev

在Java中将元素添加到Iterable

来自分类Dev

在python中将元素添加到矩阵

来自分类Dev

在Java中将整数添加到数组

来自分类Dev

在JavaScript中将变量添加到数组

来自分类Dev

在Swift中将Bool值添加到矩阵数组(二维数组)的每个元素

来自分类Dev

如何在PowerShell中将元素添加到空XML(CSCFG)节点

来自分类Dev

如何在PowerShell中将元素添加到空XML(CSCFG)节点

来自分类Dev

将元素添加到数组

来自分类Dev

将元素添加到numpy数组

来自分类Dev

将元素添加到空数组

来自分类Dev

将元素添加到数组的开头。

来自分类Dev

随后将元素添加到数组

来自分类Dev

动态将元素添加到数组

来自分类Dev

无法将元素添加到数组

来自分类Dev

将数组元素添加到PSObject

来自分类Dev

将元素添加到多维数组

来自分类Dev

将元素添加到数组的末尾

来自分类Dev

随后将元素添加到数组

来自分类Dev

将元素添加到 Firestore 数组