Background
As I went back and started closing out the many Google Chrome Windows and Tabs that I had opened, I took another look at a blog post titled “A Taste of PowerShell – String Manipulation: Splitting and Joining Strings” –http://tasteofpowershell.blogspot.com/2009/08/string-manipulation-splitting-and.html .
Introduction
The Blog post has a very concise code that was quite honestly over my head. It accesses a data file and read and emit the contents.
Actual Code:
Get-Content people.txt | >> %{$data = $_.split("`t"); Write-Output "$($data[1]) $($data[0]), $($data[2])"}
I tried changing it to
Get-Content people.txt | %{$data = $_.split("`t"); Write-Output "$($data[1]) $($data[0]), $($data[2])"}
so that I can see everything on one line, but I am still thinking to myself what does the % sign mean.
Later I found that it is a shortcut for iterating through a list. i.e. for / while.
Sample Rewrite attempts
Here is my attempts at rewrites.
($data[2]):
Here is what happens when we have ($data[2]) as part of Write-Output
Get-Content people.txt | %{ $data = $_.split("\s+"),3; Write-Output "First Name: ($data[2]) " }
Output:
($arrayPeople[0]):
Here is what happens when we have ($arrayPeople[0]) as part of Write-Output
(Get-Content people.txt ) | %{ $data = $_; $arrayPeople = $data -split "\s+"; Write-Host "First Name: $arrayPeople[0]"; }
Output:
($arrayPeople[0]) ($arrayPeople[1]) (arrayPeople[2]):
Here is what happens when we have ($arrayPeople[0]) ($arrayPeople[1]) ($arrayPeople[2]) as part of Write-Output
(Get-Content peoplepiped.txt ) | %{ $data = $_; $arrayPeople = $data -split '\s+'; Write-Host "($arrayPeople[0]) ($arrayPeople[1]) in ($arrayPeople[2])" ; }
Output:
Using format -f argument1, argument2, argument3 ( Works ):
Using format ( -f ) argument1, argument2, argument3
(Get-Content people.txt ) | %{ $data = $_; $arrayPeople = $data -split '\s+'; "{0} {1} in {2} " -f $arrayPeople[1], $arrayPeople[0] , $arrayPeople[2] }
Output:
Write-Host ( Works ) :
Preceding the array element with a dollar using ($) — $(arrayVar[position])
(Get-Content people.txt ) | ` %{ $data = $_; $arrayPeople = $data -split '\s+'; Write-Host "$($arrayPeople[0]) $($arrayPeople[1]) in $($arrayPeople[2])" ; }
Output:
Summary
When array elements are referenced within a Write-Host statement, please surround with dollar sign ($)….$($Array[array-element]).
Or use composite formatting.
Syntax:
"{} {1} in {2} " -f variable-1, variable-2 , variable-3
Sample:
"{0} {1} in {2} " -f $arPeople[1], $arPeople[0] , $arPeople[2]
Parting Comment
Sorry it took a bit of time to explain what one hopes will be easy to code; the idea of printing array elements.
Now when I go back and look at the earlier code, I see the importance of the $sign that precedes each array element:
Get-Content people.txt | %{$data = $_.split("`t"); Write-Output "$($data[1]) $($data[0]), $($data[2])"}
Reference
Powershell – Q/A
- Referencing Powershell array index produces unexpected results when referenced with string
http://stackoverflow.com/questions/8592677/referencing-powershell-array-index-produces-unexpected-results-when-referenced-w
Powershell – Format
- Powershell – ABCs – F is for format operator
https://devcentral.f5.com/articles/powershell-abcs-f-is-for-format-operator
Powershell – Composite Formatting
- Use Powershell to format strings with composite formatting
http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/12/use-powershell-to-format-strings-with-composite-formatting.aspx
Powershell – String Manipulation
- Taste of Powershell – String manipulation splitting and joining strings
http://tasteofpowershell.blogspot.com/2009/08/string-manipulation-splitting-and.html
Powershell – Get-Content
- Why Get Content ain’t yet friend
http://powershell.org/wp/2013/10/21/why-get-content-aint-yer-friend/comment-page-1/