From day at irmiter.com Fri Dec 10 00:35:59 2004 From: day at irmiter.com (Day Irmiter) Date: Fri Dec 10 00:36:04 2004 Subject: [Tucson-pm] while and until statements Message-ID: <41B943CF.2090205@irmiter.com> In the third edition of "Programming Perl" (the "camel book", 2000), page 115 reads as follows. "The 'while' statement repeatedly executes the block as long as EXPR is true. If the word 'while' is replaced by the word 'until', the sense of the test is reversed; that is, it executes the block only as long as EXPR remains false." Makes sense to me, except in practice the effect of the two statements is not parallel. --- CODE_1: $x = 1; while ($x != 4) { print "$x\n"; $x++; } --- OUTPUT_1: H:\Perl>perl -w temp 1 2 3 --- CODE_2: $x = 1; until ($x = 4) { print "$x\n"; $x++; } --- OUTPUT_2: H:\Perl>perl -w temp H:\Perl> In other words, with the until statement, the print statement does not execute. And yet, if the second bit of code is run under the debugger, it will be seen that $x is incremented to 4. Anybody have any comments about this? I'm running the ActiveState 5.8.4 binary under Win98, by the way. I'd be interested to know if running the above code on a Unix platform yields the same result. From waterhorse at ultrasw.com Fri Dec 10 00:57:51 2004 From: waterhorse at ultrasw.com (Paul Scott) Date: Fri Dec 10 00:57:35 2004 Subject: [Tucson-pm] while and until statements In-Reply-To: <41B943CF.2090205@irmiter.com> References: <41B943CF.2090205@irmiter.com> Message-ID: <41B948EF.3060902@ultrasw.com> Day Irmiter wrote: > In the third edition of "Programming Perl" (the "camel book", 2000), > page 115 reads as follows. > > "The 'while' statement repeatedly executes the block as long as EXPR is > true. If the word 'while' is replaced by the word 'until', the sense of > the test is reversed; that is, it executes the block only as long as > EXPR remains false." Makes sense to me, except in practice the effect of > the two statements is not parallel. > > --- > CODE_1: > $x = 1; > while ($x != 4) > { > print "$x\n"; > $x++; > } > --- > OUTPUT_1: > H:\Perl>perl -w temp > 1 > 2 > 3 > > --- > CODE_2: > $x = 1; > until ($x = 4) > { > print "$x\n"; > $x++; > } > --- > OUTPUT_2: > H:\Perl>perl -w temp > > H:\Perl> > > In other words, with the until statement, the print statement does not > execute. And yet, if the second bit of code is run under the debugger, > it will be seen that $x is incremented to 4. Anybody have any comments > about this? I'm running the ActiveState 5.8.4 binary under Win98, by the > way. I'd be interested to know if running the above code on a Unix > platform yields the same result. How about: until ($x == 4) ? Paul Scott From jas at cs.arizona.edu Thu Dec 9 21:13:18 2004 From: jas at cs.arizona.edu (Jasvir Nagra) Date: Fri Dec 10 05:19:43 2004 Subject: [Tucson-pm] while and until statements In-Reply-To: <41B943CF.2090205@irmiter.com> References: <41B943CF.2090205@irmiter.com> Message-ID: <1102648398.2002.10.camel@monk> You have a minor bug in CODE_2 which is responsible for the difference. It ought to read: $x = 1; until ($x == 4) # == tests equality, = is assignment { print "$x\n"; $x++; } If you use assignment, the $x is assigned 4 immediately which is interpreted as true so print never executes. -- Jasvir On Thu, 2004-12-09 at 23:35 -0700, Day Irmiter wrote: > In the third edition of "Programming Perl" (the "camel book", 2000), page 115 reads as follows. > > "The 'while' statement repeatedly executes the block as long as EXPR is true. If the word 'while' is replaced by the word 'until', the sense of the test is reversed; that is, it executes the block only as long as EXPR remains false." Makes sense to me, except in practice the effect of the two statements is not parallel. > > --- > CODE_1: > $x = 1; > while ($x != 4) > { > print "$x\n"; > $x++; > } > --- > OUTPUT_1: > H:\Perl>perl -w temp > 1 > 2 > 3 > > --- > CODE_2: > $x = 1; > until ($x = 4) > { > print "$x\n"; > $x++; > } > --- > OUTPUT_2: > H:\Perl>perl -w temp > > H:\Perl> > > In other words, with the until statement, the print statement does not execute. And yet, if the second bit of code is run under the debugger, it will be seen that $x is incremented to 4. Anybody have any comments about this? I'm running the ActiveState 5.8.4 binary under Win98, by the way. I'd be interested to know if running the above code on a Unix platform yields the same result. > -- Jasvir Nagra http://www.cs.auckland.ac.nz/~jas From day at irmiter.com Fri Dec 10 22:51:27 2004 From: day at irmiter.com (Day Irmiter) Date: Fri Dec 10 22:51:30 2004 Subject: [Tucson-pm] while and until statements In-Reply-To: <1102648398.2002.10.camel@monk> References: <41B943CF.2090205@irmiter.com> <1102648398.2002.10.camel@monk> Message-ID: <41BA7CCF.3010700@irmiter.com> Thank you, Jasvir, especially for clearing my misapprehension that $x++ was executing and that was how $x increased in value. Thanks again, also, to Paul Scott for kindly resolving my confusion. > You have a minor bug in CODE_2 which is responsible for the difference. > It ought to read: > > $x = 1; > until ($x == 4) # == tests equality, = is assignment > { > print "$x\n"; > $x++; > } > > If you use assignment, the $x is assigned 4 immediately which is > interpreted as true so print never executes. > > -- Jasvir > > On Thu, 2004-12-09 at 23:35 -0700, Day Irmiter wrote: > >> . . . with the until statement, the print statement does >> not execute. And yet, if the . . . code is run under the >> debugger, it will be seen that $x is incremented to 4. . . .