Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Subexpressions and Unary Operators

Reply
 
Old 03-02-2008   #1 (permalink)
Jon


 
 

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 SpecsSystem Spec
Old 03-02-2008   #2 (permalink)
Keith Hill [MVP]


 
 

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?
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

My System SpecsSystem Spec
Old 03-02-2008   #3 (permalink)
Jon


 
 

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 SpecsSystem Spec
Old 03-03-2008   #4 (permalink)
Keith Hill [MVP]


 
 

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.
Or you can do this:

$var = 0
Write-Host "$($var++;$var)"

which is bit easier to understand.

--
Keith

My System SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46