Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Subexpressions and Unary Operators

Closed Thread
 
Thread Tools Display Modes
Old 03-02-2008   #1 (permalink)
Jon
Guest


 

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


Old 03-02-2008   #2 (permalink)
Keith Hill [MVP]
Guest


 

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

Old 03-02-2008   #3 (permalink)
Jon
Guest


 

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



Old 03-03-2008   #4 (permalink)
Keith Hill [MVP]
Guest


 

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

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
CTP2 comparison operators Steve PowerShell 2 5 Days Ago 02:14 PM
$(subexpressions) again... Hans Dingemans PowerShell 4 02-13-2008 10:45 AM
Any possibility for boolean operators in message rules? geggytah Vista mail 2 05-14-2007 01:12 PM
PowerShell User Guide - Comparison Operators - error? =?Utf-8?B?RGF2aWQgSyBBbGxlbg==?= PowerShell 1 08-28-2006 04:04 PM
Bit Operators Wrighty PowerShell 8 07-04-2006 02:52 PM








Vistax64.com 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 2005-2008

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 47 48 49 50