tags:

views:

26

answers:

2

What exaxctly is happening in example 1? How is this parsed?

    # doesnt split on ,
[String]::Join(",",("aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa," + `
"aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa".Split(',') `
| foreach { ('"' + $_ + '"') }))  





#  adding ( ) does work 
[String]::Join(",",(("aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa," + `
"aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aaaaa").Split(',') `
| foreach { ('"' + $_ + '"') }))  
A: 

Your first example only has the Split method applied to the second string of a's. The parentheses are necessary for order of operations. The Split method is performed before the concatenation in your first example.

Lunatic Experimentalist
+1  A: 

In you first example you may remove the backtick, because Powershell knows that the string will continue (there is a + sign).

What posh does

  1. takes string "aaaa,aaaa..." (1) from first
  2. evaluates the expression with split - it returns array of strings (from "aaaa,...aaaa".Split(','))
  3. converts the array of strings to string, which returns again string "aaaa,...aaaa"
  4. adds results from 1. and 3.

Note: when posh converts array to string, it uses $ofs variable. You will see it better in action when you will try this code:

$ofs = "|"
[String]::Join(",", ("aaaaa,aaaaa" + "bbbb,bbbb,bbbb".Split(',') | foreach { ('"' + $_ + '"') }))  
stej