Andrew Gaffney wrote:

> WC -Sx- Jones wrote:
> > What is happening here -
> >
> > #! /usr/bin/perl
> > use strict;
> > use warnings;
> >
> > my $count;
> >
> > while(1) {
> >   (++$count) ? $count += $count-- : $count += $count++;
> >
> >   print "$count\n"; exit if $count > 60_000;
> >   sleep 1;
> > }
> >
> > __END__
> > -Sx-
>
> That is a damn good question. I'm not sure what results I was expecting when I ran 
> it, but
> it sure wasn't this:
>
> 3
> 15
> 63
> 255
> 1023
> 4095
> 16383
> 65535

I'm stumped, also.I would have expected progressive 2**n -1   As far as I can tell, 
the test
in the conditional operation should always evaluate true.  The preccedence of += and 
postfix
decrement should have decremented after the value had doubled.  After printing, the 
number
should be re-incremented   Sheesh--there no damn short-circuit on the evaluation!!  
Well, I'll
be darned!  Only one of the two alternatives will be assigned, but both are evaluated. 
 That
is why the following did not produce the same effects:

Greetings! E:\d_drive\perlStuff>perl -w
use strict;
use warnings;

my $count;

$count++;
while ($count <= 60000) {
   $count = $count * 2 - 1;
   $count = $count *
   print "$count\n";
   next unless $count <= 65534;
   $count++;
   sleep 1;
}

^Z
1
3
7
15
31
...

So---

Greetings! E:\d_drive\perlStuff>perl -w
use strict;
use warnings;

my $count;

while(1) {
   if (++$count) {
      $count += $count;
      $count--;
      my $alternative_yes = $count;
      $count += $count;
      $count++;
      my $alternative_no = $count;
   } else {
      die "The impossible has happened! $!";
   }
      print "$count\n"; exit if $count > 60_000;
   sleep 1;
}

^Z
3
15
63
255
1023
4095
16383
65535

Joseph





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to