如何解决如何读取一行中的数字列表作为数组?
$line
和 $lines
之间的区别是什么?
PS /home/nicholas/power_shell> $line = Get-Content ./line.txt
PS /home/nicholas/power_shell> $line
1 2 3
PS /home/nicholas/power_shell> $line[3]
PS /home/nicholas/power_shell> $line[2]
1 2 3
PS /home/nicholas/power_shell> $line.Length
5
PS /home/nicholas/power_shell> $lines = Get-Content ./lines.txt
PS /home/nicholas/power_shell> $lines
1
2
3
PS /home/nicholas/power_shell> $lines.Length
7
PS /home/nicholas/power_shell> $lines[4]
3
PS /home/nicholas/power_shell> $lines[3]
2
PS /home/nicholas/power_shell>
如何访问 $line
中的各个数字?具体来说,get $line[i]
号。即,指定索引处的元素。但是,$line
与 $lines
不是完全相同的数组。
解决方法
根据马蒂亚斯:
PS /home/nicholas/power_shell>
PS /home/nicholas/power_shell> $lines = Get-Content ./lines.txt
PS /home/nicholas/power_shell>
PS /home/nicholas/power_shell> $lines
1
2
3
PS /home/nicholas/power_shell>
PS /home/nicholas/power_shell> $line = Get-Content ./line.txt
PS /home/nicholas/power_shell>
PS /home/nicholas/power_shell> $line
1 2 3
PS /home/nicholas/power_shell>
PS /home/nicholas/power_shell> -split $line
1
2
3
PS /home/nicholas/power_shell>
不确定这是 bash 命令还是 powershell 命令,但是 interesting。谢谢。
这种方式对我来说更有意义:
PS /home/nicholas/power_shell>
PS /home/nicholas/power_shell> $line.split()
1
2
3
PS /home/nicholas/power_shell>
但是,当我使用 Tab 补全来查看可用选项时:
PS /home/nicholas/power_shell>
PS /home/nicholas/power_shell> $line.
Count LongLength Clear Equals GetLongLength IndexOf Set ForEach
IsFixedSize Rank Clone Get GetLowerBound Initialize SetValue
IsReadOnly SyncRoot CompareTo GetEnumerator GetType Insert ToString
IsSynchronized Add Contains GetHashCode GetUpperBound Remove Item
Length Address CopyTo GetLength GetValue RemoveAt Where
PS /home/nicholas/power_shell> $line.
未列出 split
函数(它是函数吗?)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。