当前位置: 移动技术网 > IT编程>脚本编程>Shell > Windows Powershell 创建数组

Windows Powershell 创建数组

2017年12月08日  | 移动技术网IT编程  | 我要评论

在powershell中创建数组可以使用逗号。

ps c:powershell> $nums=2,0,1,2
ps c:powershell> $nums
2
0
1
2

对于连续的数字数组可以使用一个更快捷的方法

ps c:powershell> $nums=1..5
ps c:powershell> $nums
1
2
3
4
5

数组的多态
象变量一样如果数组中元素的类型为弱类型,默认可以存储不同类型的值。

ps c:powershell> $array=1,"2012世界末日",([system.guid]::newguid()),(get-date)
ps c:powershell> $array
1
2012世界末日

guid
----
06a88783-a181-4511-9e41-2780ecbd7924

displayhint : datetime
date    : 2011/12/9 0:00:00
day     : 9
dayofweek  : friday
dayofyear  : 343
hour    : 14
kind    : local
millisecond : 910
minute   : 15
month    : 12
second   : 45
ticks    : 634590369459101334
timeofday  : 14:15:45.9101334
year    : 2011
datetime  : 2011年12月9日 14:15:45

空数组和单元素数组
空数组

ps c:powershell> $a=@()
ps c:powershell> $a -is [array]
true
ps c:powershell> $a.count
0

1个元素的数组

ps c:powershell> $a=,"moss"
ps c:powershell> $a -is [array]
true
ps c:powershell> $a.count
1

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网