why does this not evaluate to 6

Welcome Forums General PowerShell Q&A why does this not evaluate to 6

Viewing 21 reply threads
  • Author
    Posts
    • #205434
      Participant
      Topics: 25
      Replies: 39
      Points: 50
      Rank: Member

      $x = 5
      $y = $x++
      write-host $y

      why is $y evaluating to  5 and not 6?

    • #205446
      Senior Moderator
      Topics: 8
      Replies: 1215
      Points: 4,335
      Helping Hand
      Rank: Community Hero

      $x++ doesn’t return anything, instead it increments variable x’s value by 1. If you wan’t it to return, just wrap it in parenthesis.

      $y = ($x++)
      
      • This reply was modified 3 months, 1 week ago by kvprasoon. Reason: post correction
    • #205452
      Participant
      Topics: 25
      Replies: 39
      Points: 50
      Rank: Member

      I did try that also and it is still evaluates to 5

       

      • This reply was modified 3 months, 1 week ago by Shane.
    • #205458
      Moderator
      Topics: 3
      Replies: 222
      Points: 1,206
      Helping Hand
      Rank: Community Hero

      It should work, it is called Variable Squeezing in PowerShell.

      And as kvprasoon mentioned, $x++ will not return any value, it is used for self increment, so it can’t be assigned to $y.

      Either you can use $y = $x + 1 or $y = ($x++)

      Thank you.

      • This reply was modified 3 months, 1 week ago by Kiran. Reason: Spelling correction
    • #205467
      Participant
      Topics: 25
      Replies: 39
      Points: 50
      Rank: Member

      Thank you for your help

      I suspect I am doing something wrong or missing something obvious. I opened up a new shell and type the following:

      PS C:\Users\superuser> $x = 5
      PS C:\Users\superuser> $x
      5
      PS C:\Users\superuser> $y = ($x++)
      PS C:\Users\superuser> $y
      5

       

      • This reply was modified 3 months, 1 week ago by Shane.
    • #205476
      Moderator
      Topics: 3
      Replies: 222
      Points: 1,206
      Helping Hand
      Rank: Community Hero

      Okay, what is your windows? is it Server edition or Client Edition?

      I think variable squeezing won’t work in Server side PowerShell.

    • #205479
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      @Kiran does it really work for you on your systems? I checked it on my client W10 1909, Windows Powershell 5.1 or Powershell 7.0.0-rc.3. The result is the same on both versions.

      PS C:\> $x = 5
      PS C:\> $x
      5
      PS C:\> $y = ($x++)
      PS C:\> $y
      5
      
    • #205482
      Moderator
      Topics: 3
      Replies: 222
      Points: 1,206
      Helping Hand
      Rank: Community Hero

      [Edit]

      Yes, I checked it on my Windows 10 1909 (itself) and it is working on Windows PowerShell 5.1, PowerShell Core 6.2.3 and PowerShell 7 rc3 as well  and not on 6.2.3 & 7.0.0-rc3 (sorry for the little confusion)

      But not working on Window Server 2019 with Windows PowerShell 5.1

       

      • This reply was modified 3 months, 1 week ago by Kiran. Reason: Correction
    • #205485
      Participant
      Topics: 25
      Replies: 39
      Points: 50
      Rank: Member

      Mine is Windows 10 pro 1903, running powershell 5.1.

      • This reply was modified 3 months, 1 week ago by Shane.
    • #205488
      Participant
      Topics: 25
      Replies: 39
      Points: 50
      Rank: Member

      Yes, I checked it on my Windows 10 1909 (itself) and it is working on Windows PowerShell 5.1, PowerShell Core 6.2.3 and PowerShell 7 rc3 as well

      But not working on Window Server 2019 with Windows PowerShell 5.1

      interesting. Thanks

    • #205506
      Moderator
      Topics: 3
      Replies: 222
      Points: 1,206
      Helping Hand
      Rank: Community Hero

      I have double checked and noticed the below…

      Win PS 5.1 PS Core 6.2.3 PS 7.0.0-rc3
      Windows Native Console Windows Terminal (Preview) Native Console Windows Terminal (Preview) Native Console Windows Terminal (Preview)
      Windows 10 1909 YES NO NO NO NO NO
      Windows Server 2019 NO

      It looks very weird behavior.

      • This reply was modified 3 months, 1 week ago by Kiran.
    • #205530
      js
      Participant
      Topics: 29
      Replies: 814
      Points: 2,456
      Helping Hand
      Rank: Community Hero

      ++ after the variable increments after sending the value, ++ before the variable increments before sending the value.

      $x = 5
      $y = ++$x
      $y
      
      6

      There’s a similar thing in bash (or C):

      x=5
      y=$((++x))
      echo $y
      
      6
      • This reply was modified 3 months, 1 week ago by js.
      • This reply was modified 3 months, 1 week ago by js.
      • This reply was modified 3 months, 1 week ago by js.
    • #205542
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      ++ after the variable increments after sending the value, ++ before the variable increments before sending the value.

      … OK but I would expect the same behaviour on all systems and all versions. Wouldn’t you? 😉

    • #205548
      js
      Participant
      Topics: 29
      Replies: 814
      Points: 2,456
      Helping Hand
      Rank: Community Hero

      I don’t know what you mean. It works the way I said, in ps 5 and 7, with or without parentheses.

    • #205551
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      OK, but obviously it does not work as expected for all systems. Kiran confirmed it and it does not work on my system.

    • #205554
      js
      Participant
      Topics: 29
      Replies: 814
      Points: 2,456
      Helping Hand
      Rank: Community Hero

      Kirin is mistaken.

    • #205557
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Possible. 😉 @Kiran Did you reset your console on the system before you tried to confirm it?

    • #205575
      Moderator
      Topics: 3
      Replies: 222
      Points: 1,206
      Helping Hand
      Rank: Community Hero

      ++ after the variable increments after sending the value, ++ before the variable increments before sending the value.

      This is completely make sense.

    • #205578
      Moderator
      Topics: 3
      Replies: 222
      Points: 1,206
      Helping Hand
      Rank: Community Hero

      Kirin is mistaken.

      LOL. It is so weird!! Still working on my personal computer but not else where, very strange behavior.

      • This reply was modified 3 months, 1 week ago by Kiran.
    • #205665
      Participant
      Topics: 25
      Replies: 39
      Points: 50
      Rank: Member

      For what it is worth, I was going through basic code shown in the book, “Windows’ PowerShell Programming for the absolute beginner” third edition, when I found this abnormality,  They were using powershell 4 in the book.

      • This reply was modified 3 months, 1 week ago by Shane.
      • This reply was modified 3 months, 1 week ago by Shane.
    • #205686
      js
      Participant
      Topics: 29
      Replies: 814
      Points: 2,456
      Helping Hand
      Rank: Community Hero

      Maybe you have something weird in your $profile?

    • #205779
      Participant
      Topics: 6
      Replies: 93
      Points: 428
      Helping Hand
      Rank: Contributor

      Here is what I get. JS is correct.

      PS C:\> $x = 5
      PS C:\> $x
      5
      PS C:\> $y = ($x++)
      PS C:\> $y
      5
      PS C:\> $y = ($x++)
      PS C:\> $y
      6
      PS C:\> $y = ($x++)
      PS C:\> $y
      7
      PS C:\>

Viewing 21 reply threads
  • The topic ‘why does this not evaluate to 6’ is closed to new replies.