问题描述

编写PowerShell脚本,以多维(3维)数组中第二维度的值进行过滤,并打印出结果

#三维数组源数据

“A”, “11”, “Cheng Du”
“B”, “21”, “Chong Qing”
“C”, “31”, “Shang Hai”
“D”, “41”, “Bei Jing”
“E”, “51”, “Nan Jing” #从地址中过滤以Chong开头的地区, 结果需要为
B, 21, Chong Qing

PowerShell脚本为:

$CityList = [System.Collections.ArrayList]::new()
$CityList.Add(@(“A”,“11”,“Cheng Du”)) | Out-Null
$CityList.Add(@(“B”,“21”,“Chong Qing”)) | Out-Null
$CityList.Add(@(“C”,“31”,“Shang Hai”)) | Out-Null
$CityList.Add(@(“D”,“41”,“Bei Jing”)) | Out-Null
$CityList.Add(@(“E”,“51”,“Nan Jing”)) | Out-Null Write-Host "==== 开始过滤 Chong ==== " -ForegroundColor DarkYellow
$FinalCityList = $CityList | Where-object -filterScript {$_[2] -like "Chong*"} Write-Host "Final City List 类型" -ForegroundColor Green
$FinalCityList.GetType() Write-Host "Final City 数量为: " $FinalCityList.Count -ForegroundColor Green
Write-Host ""
Write-Host "==== 循环打印List中所有元素结果 ==== " -ForegroundColor DarkYellow
foreach( $aaa in $FinalCityList)
{
Write-Host $aaa[0]','$aaa[1]','$aaa[2]
} Write-Host ""
Write-Host "==== 直接输出对象的结果 ==== " -ForegroundColor red
$FinalCityList

输出结果 :当过滤结果大于1时,显示正常,当过滤结果为1时,结果显示异常。结果变成了一个一位数组,Count结果为3。

这是为什么呢?

问题分析

从 $FinalCityList 的打印结果分析,当 Where-Object 查看到结果只有一个的时候,就把结果对象进行了多维到一维的转换。所以结果变为了一个包含三行内容的一位数组。

问题解决

$FinalCityList = @($CityList | Where-object -filterScript {$_[2] -like "Chong*"})

把 Where-Object 方法用 @()  进行对象转换即可。

完整的PowerShell脚本为:

$CityList = [System.Collections.ArrayList]::new()
$CityList.Add(@(“A”,“11”,“Cheng Du”)) | Out-Null
$CityList.Add(@(“B”,“21”,“Chong Qing”)) | Out-Null
$CityList.Add(@(“C”,“31”,“Shang Hai”)) | Out-Null
$CityList.Add(@(“D”,“41”,“Bei Jing”)) | Out-Null
$CityList.Add(@(“E”,“51”,“Nan Jing”)) | Out-Null
foreach( $ccc in $CityList)
{
Write-Host $ccc[0]','$ccc[1]','$ccc[2]
} Write-Host "==== 开始过滤 CityList 中包含 Chong 的城市 ==== " -ForegroundColor Yellow
$FinalCityList = @($CityList | Where-object -filterScript {$_[2] -like "Chong*"}) Write-Host "Final City List 类型" -ForegroundColor Green
$FinalCityList.GetType() Write-Host "Final City 数量为: " $FinalCityList.Count -ForegroundColor Green
Write-Host ""
Write-Host "==== 循环打印List中所有元素结果 ==== " -ForegroundColor DarkYellow
foreach( $aaa in $FinalCityList)
{
Write-Host $aaa[0]','$aaa[1]','$aaa[2]
} Write-Host "" Write-Host "==== 直接输出对象的结果 ==== " -ForegroundColor red
$FinalCityList

参考文档

数组子表达式运算符:  https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_arrays?view=powershell-7.2#the-array-sub-expression-operator

What does the "@" symbol do in PowerShell?:https://stackoverflow.com/questions/363884/what-does-the-symbol-do-in-powershell

最新文章

  1. Sharing A Powerful Tool For Calculate Code Lines
  2. online judge(ACM) 的设计与分析 (有c#demo)
  3. show processlist 命令详解
  4. js数组内置方法
  5. MathType支持64位 WIN 7Office 2013(完美解决)(转载)
  6. MFC socket网络通讯核心代码
  7. 红豆带你从零学C#系列之:开始C#编程(一)
  8. pig询问top k,每个返回hour和ad_network_id最大的两个记录(SUBSTRING,order,COUNT_STAR,limit)
  9. 第一百二十七节,JavaScript,JSON数据类型转换,数据转换成字符串,字符串转换成数据
  10. 使用Nginx实现灰度发布
  11. WPF ViewModel与多个View绑定后如何解决的问题
  12. Mysql大量插入数据时SQL语句的优化
  13. 关于 IOS 时间的一下用法
  14. currentStyle&getComputedStyle获取属性
  15. add_polygon_to_map
  16. ComboBox Style
  17. Git分支模型
  18. The.first.glance.at.linux.commands
  19. Class.forName()用法
  20. 深入理解Java的注解(Annotation):基本概念(1)

热门文章

  1. 计算机网络-IP篇
  2. Qt:QWebEngineView
  3. Python 完美诠释"高内聚"概念的 IO 流 API 体系结构
  4. vue监听有无网络
  5. 『现学现忘』Docker基础 — 14、Docker的卸载
  6. laravel 公共类json库封装
  7. 题目要求:传入数组 内容为[['lisi','男','27'],['wangwu','男',18],['zhaoliu','男','30']],将此二维数组转化为一维数组,创建自定义函数完成
  8. CodeReview规范
  9. ASP.NET Core 6框架揭秘实例演示[21]:如何承载你的后台服务
  10. 路由的query参数(传参)