• Split?

    From Tom@21:1/5 to All on Tue Jun 4 10:22:38 2019
    Greetings,

    I have a file with:

    Y21 M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved
    Y22 M3_TRSTB JTAG_TRSTB/M3_TRSTB Reserved

    I have the following code to read the file then try to process
    each element on the line:

    $a = Get-Content -Path .\pin.txt

    foreach ($line in $a) {
    #$element = $line.Split(" ")
    $element = $line -split "\s+"
    Write-Host "E1: $element[0]"
    }

    However the .Split or -split doesn't work? I get:

    E1: Y21 M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved[0]
    E1: Y22 M3_TRSTB JTAG_TRSTB/M3_TRSTB Reserved[0]

    I've tied to convert to a string or pipe to Out-String on the reading, but that didn't work. Any idea what I'm doing wrong? Why is it not spliting the line variable into an array?

    Thanks in advance for any help!
    Tom

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chad Rexin@21:1/5 to Tom on Thu Aug 8 15:51:01 2019
    On Tuesday, June 4, 2019 at 12:22:40 PM UTC-5, Tom wrote:
    Greetings,

    I have a file with:

    Y21 M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved
    Y22 M3_TRSTB JTAG_TRSTB/M3_TRSTB Reserved

    I have the following code to read the file then try to process
    each element on the line:

    $a = Get-Content -Path .\pin.txt

    foreach ($line in $a) {
    #$element = $line.Split(" ")
    $element = $line -split "\s+"
    Write-Host "E1: $element[0]"
    }

    However the .Split or -split doesn't work? I get:

    E1: Y21 M3_SWO JTAG_TDO/M3_TDO/M3_SWO Reserved[0]
    E1: Y22 M3_TRSTB JTAG_TRSTB/M3_TRSTB Reserved[0]

    I've tied to convert to a string or pipe to Out-String on the reading, but that didn't work. Any idea what I'm doing wrong? Why is it not spliting the line variable into an array?

    Thanks in advance for any help!
    Tom

    I believe this has to do with how the Write-host is working. You should get what you are expecting if you tell it to process the variable and then include that in the string. This would be done with the Write-Host line becoming like this, noting that
    the $() syntax is what tells it to process that variable first before doing the write-host/string processing. There's probably a better way of explaining this, but I tested this with your example and it will return "E1: Y21" when you use the Write-Host
    line below.

    Write-Host "E1: $($element[0])"

    Chad

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)