如何读取一行中的数字列表作为数组? [英] How to read a list of numbers on a single line in as an array?

查看:26
本文介绍了如何读取一行中的数字列表作为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$line$lines 之间的区别是什么?

What's the distinction here between $line and $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 中的单个数字?具体来说,要获取$line[i] 编号.即,指定索引处的元素.然而,$line$lines 并不是一个数组.

How are the individual numbers in $line accessed? Specifically, to get the $line[i] number. That is, the element at the specified index. However, $line isn't an array in quite the same way as $lines is.

推荐答案

per Mathias:

per Mathias:

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 命令,但有趣.谢谢.

not sure if that's a bash or powershell command, but interesting. thx.

这种方式对我来说更有意义:

this way makes more sense to me:

PS /home/nicholas/power_shell> 
PS /home/nicholas/power_shell> $line.split()


1
2
3


PS /home/nicholas/power_shell> 

但是,当我使用 Tab 补全来查看可用选项时:

However, when I use tab completion to see the available options:

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 函数(它是一个函数吗?)未列出.

the split function (is it a function?) isn't listed.

这篇关于如何读取一行中的数字列表作为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆