none
Different results ISE vs console - op_addition with multi-line strings. RRS feed

  • Question

  • Hi, I have this simple test.ps1 script:

    $message = "AB"
    
    $c_A=@'
    AAAAA
    AAAAA
    AAAAA
    '@
    $c_B=@'
    BBBBB
    BBBBB
    BBBBB
    '@
    
    $FinalText = $null
    $MessageArray = $Message.ToCharArray()
        for($i = 0; $i -le 4; $i++){
            foreach ($char in $MessageArray){ 
    
                $FinalText += (((gv c_$char).value) -split "`n")[$i]
    
            }
            $FinalText += "`n"
        }
    $finaltext
    pause
    


    The Result in ISE is:
    AAAAABBBBB
    AAAAABBBBB
    AAAAABBBBB

    If I copy-paste the code to the console, the result is the same. So far, so good.

    But if I right-click and run test.ps1 script, the result is different:
    BBBBB
    BBBBB
    AAAAABBBBB

    Why is that? 

    the op_addition works fine on ISE, but is not working as expected on console. How can I make it work the same on both ISE and console?

    Friday, February 16, 2018 5:57 PM

Answers

  • Change `n to `r`n in your code.

    They are not equivalent.


    -- Bill Stewart [Bill_Stewart]

    Thursday, February 22, 2018 2:48 PM
    Moderator

All replies

  • Hi, I have this simple test.ps1 script:

    $message = "AB"

    $c_A=@'
    AAAAA
    AAAAA
    AAAAA
    '@
    $c_B=@'
    BBBBB
    BBBBB
    BBBBB
    '@

    $FinalText = $null
    $MessageArray = $Message.ToCharArray()
        for($i = 0; $i -le 4; $i++){
            foreach ($char in $MessageArray){ 

                $FinalText += (((gv c_$char).value) -split "`n")[$i]

            }
            $FinalText += "`n"
        }
    $finaltext
    pause

    The Result in ISE is:
    AAAAABBBBB
    AAAAABBBBB
    AAAAABBBBB

    If I copy-paste the code to the console, the result is the same. So far, so good.

    But if I right-click and run test.ps1 script, the result is different:
    BBBBB
    BBBBB
    AAAAABBBBB

    Why is that? 

    the op_addition works fine on ISE, but is not working as expected on console. How can I make it work the same on both ISE and console?


    • Merged by jrv Friday, February 16, 2018 7:10 PM DUPLICATE
    Friday, February 16, 2018 5:41 PM
  • Please post code correctly.   The code you posted does not work at all because it cannot be copied and pasted.

    \_(ツ)_/

    Friday, February 16, 2018 7:04 PM
  • I don't understand. I was able to copy-paste the code and run it.

    $message = "AB"
    
    $c_A=@"
    AAAAA
    AAAAA
    AAAAA
    "@
    $c_B=@"
    BBBBB
    BBBBB
    BBBBB
    "@
    
    $FinalText = $null
    $MessageArray = $Message.ToCharArray()
        for($i = 0; $i -le 4; $i++){
            foreach ($char in $MessageArray){ 
    
                $FinalText += (((gv c_$char).value) -split "`n")[$i]
    
            }
            $FinalText += "`n"
        }
    $finaltext
    pause

    Friday, February 16, 2018 7:49 PM
  • Don't copy-and-paste to run. Put the code in a script so that a new scope is created when you run it.

    1. Open powershell.exe and run the script.

    2. Open powershell_ise.exe and run the script.

    Note that the output will be identical.


    -- Bill Stewart [Bill_Stewart]

    Friday, February 16, 2018 8:29 PM
    Moderator
  • I did that and the results are still different.

    Friday, February 16, 2018 10:46 PM
  • You are definitely doing something different than what I posted, because I can't reproduce it.

    -- Bill Stewart [Bill_Stewart]

    Friday, February 16, 2018 11:20 PM
    Moderator
  • Fixing the post allows me to copy and paste correctly.  The failure is browser specific.  Some browsers don't inject extra characters  but most do.  The code box prevents this because the text is not copied as HTML.


    \_(ツ)_/

    Saturday, February 17, 2018 4:24 AM
  • So, did you see that the result on ISE is different from console? Why is that?

    ISE: 
    AAAAABBBBB
    AAAAABBBBB
    AAAAABBBBB

    Console: 
    BBBBB
    BBBBB
    AAAAABBBBB


    Wednesday, February 21, 2018 9:47 PM
  • Change `n to `r`n in your code.

    They are not equivalent.


    -- Bill Stewart [Bill_Stewart]

    Thursday, February 22, 2018 2:48 PM
    Moderator
  • Fantastic, it worked! I know carriage return but I didn't believe it would make any difference.

    Now it works on both console and ISE. Marked as answer.

    Thursday, February 22, 2018 10:35 PM