![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Subexpressions and Unary Operators Hi I'm trying to work out the difference in the behaviour of $() and () - in particular with regard to unary operators, and would appreciate any insights on the matter. #These statements work as expected .... $i=3; Write-Host ($i++) $i=3; Write-Host (++$i) #but these show no output $i=3; Write-Host $($i++) $i=3; Write-Host $(++$i) $i=3; $i++ $i=3; ++$i #whereas these do ... $i=3; Write-Host $(($i++)) $i=3; Write-Host $((++$i)) Any thoughts? -- Jon |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Subexpressions and Unary Operators "Jon" <Email_Address@xxxxxx> wrote in message news:OvU36gLfIHA.2000@xxxxxx Quote: > > Hi > > I'm trying to work out the difference in the behaviour of $() and () - in > particular with regard to unary operators, and would appreciate any > insights on the matter. > > > #These statements work as expected .... > > $i=3; Write-Host ($i++) > $i=3; Write-Host (++$i) > > #but these show no output > > $i=3; Write-Host $($i++) > $i=3; Write-Host $(++$i) > $i=3; $i++ > $i=3; ++$i > > > #whereas these do ... > > $i=3; Write-Host $(($i++)) > $i=3; Write-Host $((++$i)) > > > Any thoughts? basically comes down to PowerShell making assumptions about whether or not you want to see the result of an increment/decrement operation. Most of the time you don't. However () is special since you can only have one expression in it and if they followed their normal rules then ($i++) wouldn't output anything. The language designers figured that would be "unexpected" in this scenario so they make an exception. HTH, Keith |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Subexpressions and Unary Operators "Keith Hill [MVP]" <r_keith_hill@xxxxxx_spam_I> wrote in message news:E71EA756-D026-4A25-A642-4F9096F4D22A@xxxxxx Quote: > Bruce Payette talks about this in his book Windows PowerShell in Action. > It basically comes down to PowerShell making assumptions about whether or > not you want to see the result of an increment/decrement operation. Most > of the time you don't. However () is special since you can only have one > expression in it and if they followed their normal rules then ($i++) > wouldn't output anything. The language designers figured that would be > "unexpected" in this scenario so they make an exception. > > HTH, > Keith Thanks Keith - one of the books on my "to read " list. It sounds like it's been reasoned through then. It's slightly different from what I've experienced in other languages, hence the question. I suppose as it stands now, the flexibility is there to either output or not output as you choose. Hopefully that flexibility will remain, since it's a useful construction with commands like Write-Host - otherwise you're forced to use roundabout techniques like $var = 3; $var = $var + 1 Write-Host "Var is now $var" #More compact, as you can now, to be able to write $var = 3 Write-Host "Var is now $((++$var))" albeit a little unintuitive. -- Jon |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Subexpressions and Unary Operators "Jon" <Email_Address@xxxxxx> wrote in message news:u5Dew#LfIHA.4712@xxxxxx Quote: > Thanks Keith - one of the books on my "to read " list. It sounds like it's > been reasoned through then. > > It's slightly different from what I've experienced in other languages, > hence the question. I suppose as it stands now, the flexibility is there > to either output or not output as you choose. Hopefully that flexibility > will remain, since it's a useful construction with commands like > Write-Host - otherwise you're forced to use roundabout techniques like > > $var = 3; $var = $var + 1 > Write-Host "Var is now $var" > > #More compact, as you can now, to be able to write > $var = 3 > Write-Host "Var is now $((++$var))" > > albeit a little unintuitive. $var = 0 Write-Host "$($var++;$var)" which is bit easier to understand. -- Keith |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| redirection operators as syntactic suger | PowerShell | |||
| Powershell Operators | PowerShell | |||
| CTP2 comparison operators | PowerShell | |||
| $(subexpressions) again... | PowerShell | |||
| Bit Operators | PowerShell | |||