【问题标题】:add element to array in powershell在powershell中将元素添加到数组
【发布时间】:2019-11-22 16:08:14
【问题描述】:

我正在尝试将总计行添加到数组 $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;

【问题讨论】:

  • 也许你应该试试$response_table += [pscustomobject]@{Name="Total"; Count=55}。这样您就可以添加一个具有您定义的属性的自定义对象,而不仅仅是一个哈希表。可能有比使用+= 更好的方法。这导致每次都构建整个数组并且效率低下。
  • @AdminOfThings 你最后一点的答案是使用System.Collections.Generic.List[type]

标签: powershell


【解决方案1】:

正如AdminOfThings 所指出的,您错误地将 hashtable 添加到现有的 自定义对象 数组(Select-Object 输出)。

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

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

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

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


一般避免使用+= 附加到数组:

数组是固定大小的数据结构,所以当你用+=“追加”一个数组时,PowerShell必须做的是每次在后台创建一个new数组,这很循环效率低。

虽然使用有效的就地可扩展数据结构(例如 [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}

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

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 2020-03-16
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多