tag:blogger.com,1999:blog-5384857.post7102984638446868097..comments2023-10-10T03:52:55.494+01:00Comments on Under The Stairs: Using PowerShell’s ++ (and–-) operator–Take CareUnknownnoreply@blogger.comBlogger6125tag:blogger.com,1999:blog-5384857.post-91144686653712539412021-02-03T21:15:28.063+00:002021-02-03T21:15:28.063+00:00Awesome comment, Conrad Braam. The observation th...Awesome comment, Conrad Braam. The observation that $(++$i) essentially yields $null, and the discovery of the [int] workaround, are both crucial, and somewhat bizarre.<br /><br />So this works:<br /><br /> "{0}: hello" -f $i++<br /><br />But this does not:<br /><br /> "$($i++): hello"<br /><br />But then this does:<br /><br /> "$([int]$i++): hello"<br /> NeoDoghttps://www.blogger.com/profile/16435454006686614425noreply@blogger.comtag:blogger.com,1999:blog-5384857.post-9288605473040625712017-07-03T15:16:53.357+01:002017-07-03T15:16:53.357+01:00Agree, the c/C++ implementation is already well un...Agree, the c/C++ implementation is already well understood but foudn a curious side-effect<br />I found this particularly insidious behaviour around the operator not returning an rvalue (right-hand side evaluation) in a way you might expect it to do.<br />basically... the easiest way to show this is in how<br />$lc=0;1..10 | foreach {"Loop counter:" + ++$lc }<br />is completely different to<br />$lc=0;1..10 | foreach {"Loop counter:" + $(++$lc) } # in case i wanted to write some other manipulations here, the parse gets all smart on me because i scoped the value in a $()<br />which prints this:<br />Loop counter:<br />Loop counter:<br />Loop counter:<br />Loop counter:<br />Loop counter:<br />Loop counter:<br />Loop counter:<br />Loop counter:<br />Loop counter:<br />Loop counter:<br />And it's because the parser is doing something unusual if you assume you get an rvalue. I can see why the parser wont generate an rvalue if you wrote this code<br />$lc++<br />Standing all on it's own you would not want to generate an rvalue at all, since that would join the pipeline unexpectedly, something the C compiler knows to gobble up at parse-time because there is no lvalue container.<br />I found a way to get it to keep the C/C++ behaviour in a one-liner though<br />$lc=0;1..10 | foreach {"Loop counter:" + $([int]++$lc) }<br />Conrad Braamhttps://www.blogger.com/profile/14516128061510018135noreply@blogger.comtag:blogger.com,1999:blog-5384857.post-52079487835229321722017-07-03T12:57:09.961+01:002017-07-03T12:57:09.961+01:00The only gotcha is, that $variable++ does not rval...The only gotcha is, that $variable++ does not rvalue.<br />in C/C++ the pre and post operators actually return a right-hand side value, which make this kind of logic possible<br />if ( $($variable++) -eq 42) {'We hit 42'}<br />will never happen, since the pre and post operators don't actually 'return' anything in powershell 4 you cannot test the variable...Conrad Braamhttps://www.blogger.com/profile/14516128061510018135noreply@blogger.comtag:blogger.com,1999:blog-5384857.post-5758287198034024892011-12-17T18:02:51.811+00:002011-12-17T18:02:51.811+00:00No need to take care once you understand the histo...No need to take care once you understand the history of the operators.<br /><br />They were first used extensively in 'C" lqnguage. The pre and post use have a very specific and desired effect that we use all of the time.<br /><br />Try the following and you will immediately see the difference.<br /><br /><b>$lc=0;1..10 | foreach {"Loop counter:" + $lc++ }<br /><br />$lc=0;1..10 | foreach {"Loop counter:" + ++$lc }</b><br /><br />Look closely at the output. If you only print teh counter after the loop has finished it will always appear to be 10 but outside the loop we have incremented one extra time with post increment. That is because post-increment increments teh counter AFTER the couter has been printed inside the loop. IN most cases we want this behavior.<br /><br />for($i-0;$i -lt 10; $i++){....}<br /><br />With ++$lc we increment before we print and the ending counter is always the same as the last one printed.<br /><br />Here is one explanation of the operators: <a href="http://www.techotopia.com/index.php/Basic_Windows_PowerShell_1.0_Operators" rel="nofollow">Pre-Post OPerators</a><br /><br />They are very useful once you understand how they work.jvhttps://www.blogger.com/profile/05792438863044071854noreply@blogger.comtag:blogger.com,1999:blog-5384857.post-54191226555307576292011-12-13T01:27:16.583+00:002011-12-13T01:27:16.583+00:00That's exactly how C/C++ and related languages...That's exactly how C/C++ and related languages have had ++ implemented as well for a couple decades or more now, I'm sure PowerShell's designers/thinkers/implementers were just conforming to that spec.Jason Turnagehttps://www.blogger.com/profile/13329823318023697063noreply@blogger.comtag:blogger.com,1999:blog-5384857.post-81807659857397175542011-12-12T16:26:30.834+00:002011-12-12T16:26:30.834+00:00That's not just a PowerShell curiousity...it&#...That's not just a PowerShell curiousity...it's how ++ and -- work in many languages. If you use them before the variable, the increment/decrement happens and then the new value is returned. If you use them after the variable, the current value is returned, and then the increment/decrement happens.Unknownhttps://www.blogger.com/profile/11454393748949550662noreply@blogger.com