string list to a single-column array

This topic contains 9 replies, has 4 voices, and was last updated by  John Curtiss 6 days, 6 hours ago.

  • Author
    Posts
  • #80899

    John Curtiss
    Participant

    say somebody emailed me this text:

    item1
    item2
    item3

    if I save it as a txt file and point get-content at that text file, I get an array with 3 rows.

    what's the easiest way to get that same array if I just want to paste that text list directly into the ISE instead of saving it into a text file and pointing get-content at that text file?

  • #80902

    Richard Siddaway
    Moderator

    This worked in my tests

    $x  = @'
    item1
    item2
    item3
    '@ -split "`n"
    
    $x.length
    
    • #80906

      John Curtiss
      Participant

      thank you Richard. the @'....'@ syntax was what I was missing. I was bringing the list in as a pure "string":

      $x = "item1
      item2
      item3"

      and then splitting it. your way is much cleaner. with the added "copying and pasting ease/laziness" benefit of not having to have the first and last items in the list on the same line as the quotation marks.

  • #80903

    Naw Awn
    Participant

    I would Ctrl+C & Ctrl+V haha Or...
    ise C:\temp\Saved.txt Or are you looking into more with manipulating Clip.exe?
    https://brianreiter.org/2010/09/03/copy-and-paste-with-clipboard-from-powershell

  • #80911

    Naw Awn
    Participant

    Ah I get it. "Here String" is what you are looking for.

  • #81193

    John Curtiss
    Participant

    i'm back. if I paste Richard's whole script into the ISE, and then add this to the bottom

    $x[0].Length

    I get 5. which is correct.

    if I type item1, item2, item3 into the here-string, I also get 5.

    but if I copy and paste

    item1
    item2
    item3

    from anywhere on this page into my here-string, the length turns into 6. even if I paste it into notepad first and then copy it from notepad into the ISE.

  • #81194

    Curtis Smith
    Participant

    Hi John,
    You never really asked a question, but I assume the question is why you are getting 6 instead of 5.

    It is mostly likely that you are not accounting for the carriage return. Try doing the following instead to account for the carriage return as well as the line feed.

    $x  = @'
    item1
    item2
    item3
    '@ -split "`r`n"
    
    $x.length
    $x[0].length
  • #81250

    John Curtiss
    Participant

    Curtis, I just pasted your whole script into ise, and $x.length came back as 1, and $x[0].length came back as 17.

    if I use "`r|`n" as the split, then $x.length is correctly 3 and $x[0].length is correctly 5.

    but again, if I paste

    item1
    item2
    item3

    into the here-string from notepad, $x.length becomes 5. so there are two extra elements in the array, which I can also see if I do $x | out-gridview (there are blank lines)

  • #81436

    Curtis Smith
    Participant

    Hey John,
    I was able to reproduce similar results, but not all the scenarios are the same for me as what you have represented.

    In any case, you can see what is going on by doing something like this

    $x  = @'
    item1
    item2
    item3
    '@ -split "`n"
    
    Write-host "Array length: " -ForegroundColor Cyan -NoNewline
    $x.length
    
    Write-host "Element 0 length: " -ForegroundColor Cyan -NoNewline
    $x[0].length
    
    Write-host "Element 0 Character list and integer value:" -ForegroundColor Cyan
    $x[0].tochararray() | % {"$_ : $([int][char]$_)"}

    Results:

    Array length: 3
    Element 0 length: 6
    Element 0 Character list and integer value:
    i : 105
    t : 116
    e : 101
    m : 109
    1 : 49
     : 13

    Notice in my case at the end of the string there is an ASCII character 13 "Carriage Return"
    http://www.asciitable.com/

    You can use this technique to determine what that sixth character is you can't see.

  • #81763

    John Curtiss
    Participant

    Curtis, you went back to only splitting it by the newline. not by newline and carriage return.

    in any event, if I paste your new script from this site into my 5.1 ise, I get an array with a length of 3 and element 0 has a length of 5. no extra characters.

    interestingly, if I copy out the three members of your here-string from my ISE and paste them into notepad, they appear on one line with no spaces:

    item1item2item3

    but if I then copy that out of notepad, and paste it in this thread, or into another ise window, they are stacked with carriage returns (or new lines, whichever it is).
    item1
    item2
    item3

    if I type my own into notepad:

    item1
    item2
    item3

    and then paste that into the here-string in your script in my ISE, I get the same results you got. array length 3, element 0 length 6 with an extra character 13 on the end.

    good times! 🙂

You must be logged in to reply to this topic.