PS笔记:What's the difference between Subexpression and Array Subexpression?
来源:互联网 发布:网络用语2015最新 编辑:程序博客网 时间:2024/06/05 19:59
关于powershell的细节十分的多,作为回顾和总结,今天我们要说的是关于子表达式(Subexpression)和数组子表达式(Array Subexpression)的区别。
先来简单介绍下 两者表达式的主要含义:
$( ) 子表达式运算符 说明:返回一条或多条语句的结果。对于单个结果将返回标量。对于多个结果将返回数组。
@( ) 数组子表达式运算符 说明:通过数组返回一条或多条语句的结果。如果只有一项,则数组只有一个成员。
在子表达式中你可以任意添加语句,甚至包括循环语句,因为在子表达式运算符中是全封闭的,比如:
$num = 0
$x = $($num++)
$x
最后很多人一定认为$x 等于1,可是正确的结果为 0,因为如之前所说在子表达式运算符中是全封闭的,而后递增操作符在运行之后就把结果消除了。
在看看这个例子:
$num = 0
$x = $($num++)
$x = $($num)
$x
结果为 1
好了,开始着重说明两个表达式的区别,这才是今天本文的重点,请看如下例子:
$("bbb","aaa","ccc" | sort )[0]
会返回aaa
那么$("aaa" | sort )[0]会返回什么结果呢,出乎意料的是会返回a,这是为什么呢,为什么不是众多人所希望的aaa呢?因为根据子表达式运算符的说明:对于单个结果将返回标量。对于多个结果将返回数组。而这里讲会返回单个结果aaa的标量0,aaa是一个字符串,所以当你取下标为0时,自然会返回字符a。
那么如何才能做到我们想要得到字符串aaa呢,这时候就可以使用数组子表达式,回顾下我们之前所说的数组子表达式的说明:通过数组返回一条或多条语句的结果。如果只有一项,则数组只有一个成员。也就是说我们只有一项的时候aaa就会成为这个数组里的一个成员而不是被拆分为一个个字符下标。
@("bbb","aaa","ccc" | sort )[0]
结果aaa
好了这就是我们想要的结果~
- PS笔记:What's the difference between Subexpression and Array Subexpression?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- What's the difference between an event and a delegate?
- what's the difference between OnDraw and OnPaint
- What's the difference between stack and heap?
- What's the difference between LastWriteTime and ChangeTime in FILE_BASIC_INFO?
- What's the difference between Heap and Stack
- What's the difference between overload and override
- What's the difference between glibc and libc6?
- What's the difference between "but" and "yet"
- What's+the+difference+between+virtualization+and+cloud+computing?
- What's the difference between parallel and concurrent?
- What's the difference between program arguments and VM arguments?
- What's the difference between Template and Table in Smartforms
- What's the difference between “local” and “my” in Perl
- what's the difference between int (* f [])(); and int f[]();
- What's the difference between gcc and g++/gcc-c++?
- What's the difference between headsets and headphones?
- 快速转化J2ME程序为Android程序
- linux mmap 使用
- Iterator 接口
- Cannot find ActionMappings or ActionFormBeans collection
- java关于File类的介绍
- PS笔记:What's the difference between Subexpression and Array Subexpression?
- 汇编原理之前言
- [转]学习linux/unix编程方法的建议
- 上传文件
- NIO就绪处理之OP_WRITE
- MATLAB拉格朗日插值
- MATLAB 牛顿迭代算法
- Drupal: 面向对象的设计理念
- Odyssey Access Client