【Azure Developer】使用PowerShell Where-Object方法过滤多维ArrayList时候,遇见的诡异问题 -- 当查找结果只有一个对象时,返回结果修改了对象结构,把多维变为一维
2024-09-19 18:17:37
问题描述
编写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
参考文档
What does the "@" symbol do in PowerShell?:https://stackoverflow.com/questions/363884/what-does-the-symbol-do-in-powershell
最新文章
- Sharing A Powerful Tool For Calculate Code Lines
- online judge(ACM) 的设计与分析 (有c#demo)
- show processlist 命令详解
- js数组内置方法
- MathType支持64位 WIN 7Office 2013(完美解决)(转载)
- MFC socket网络通讯核心代码
- 红豆带你从零学C#系列之:开始C#编程(一)
- pig询问top k,每个返回hour和ad_network_id最大的两个记录(SUBSTRING,order,COUNT_STAR,limit)
- 第一百二十七节,JavaScript,JSON数据类型转换,数据转换成字符串,字符串转换成数据
- 使用Nginx实现灰度发布
- WPF ViewModel与多个View绑定后如何解决的问题
- Mysql大量插入数据时SQL语句的优化
- 关于 IOS 时间的一下用法
- currentStyle&;getComputedStyle获取属性
- add_polygon_to_map
- ComboBox Style
- Git分支模型
- The.first.glance.at.linux.commands
- Class.forName()用法
- 深入理解Java的注解(Annotation):基本概念(1)
热门文章
- 计算机网络-IP篇
- Qt:QWebEngineView
- Python 完美诠释";高内聚";概念的 IO 流 API 体系结构
- vue监听有无网络
- 『现学现忘』Docker基础 — 14、Docker的卸载
- laravel 公共类json库封装
- 题目要求:传入数组 内容为[['lisi','男','27'],['wangwu','男',18],['zhaoliu','男','30']],将此二维数组转化为一维数组,创建自定义函数完成
- CodeReview规范
- ASP.NET Core 6框架揭秘实例演示[21]:如何承载你的后台服务
- 路由的query参数(传参)