0

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> 

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.

1 Answers1

0

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> 

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> 

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.

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

  • 1
    [-split](https://learn.microsoft.com/en-US/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.1#split-and-join-operators) is a PowerShell operator – Theo Jul 19 '21 at 11:13
  • The split method is available to strings – Abraham Zinala Jul 19 '21 at 12:19