使用两个等长数组执行数学运算 [英] Perform math operations with two equal length arrays

查看:64
本文介绍了使用两个等长数组执行数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的事情是如此简单,以至于我一直在努力寻找答案.我正在尝试互相减去两个等长的数组

What I'm doing is so simple that I'm struggling to find an answer for it. I'm trying to subtract two equal length arrays from one another

$free_array = get-wmiobject -class win32_logicaldisk | select -ExpandProperty freespace
$size_array = get-wmiobject -class win32_logicaldisk | select -ExpandProperty size

ForEach ($size in $size_array)
  {
    Write-Host Statistic: $size - $freespace
  }

推荐答案

我不认为PowerShell具有内置功能可以同时跨多个数组进行映射,因此可以使用

I don't think that PowerShell has a built-in function to map across multiple arrays simultaneously and so instead you can use a range operator and then index into both arrays as needed:

foreach ($Index in (0..($free_array.Count - 1))) {
    Write-Host Statistic: ($size_array[$Index] - $free_array[$Index])
}

但是,您的任务也可以像这样完成,我认为这样可读性更强:

However, your task could also be done like this which I think would be more readable:

$LogicalDisks = Get-CimInstance -ClassName Win32_LogicalDisk

foreach ($LogicalDisk in $LogicalDisks) {
    Write-Host Statistic: ($LogicalDisk.Size - $LogicalDisk.FreeSpace)
}

这篇关于使用两个等长数组执行数学运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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