[oak perl] until loop(s)

David Alban extasia at gmail.com
Mon Mar 28 12:48:12 PST 2005


Greetings,

The test in the until loop "my $counter == 5" declares $counter and
then compares its value to 5 without having first initialized it.  And
undef never equals 5.  So you'll never satisfy the test.

So each time through the until loop (which occurs only when the
stdin's eof is reached in the while loop) you're testing a completely
new $counter that never existed.

Did you "use warnings"?  It would tell you:

  Use of uninitialized value in numeric eq (==) at foo line 4.

David

On Mon, 28 Mar 2005 15:13:17 -0500, Sandy Santra <santranyc at yahoo.com> wrote:
> I'm learning (struggling with) "until" loops today.  I want an "until"
> control structure that "breaks out" when a condition is fulfilled.
> 
> Can anyone find the problem in the below loop?  (It never breaks out.)  Thanks.
> 
> use strict;
> print "Can you create world peace in five tries?  Type a solution below:\n";
> my $number;
> until (my $counter == 5)
> 
> {
>          while ($number=<STDIN>)
>          {
>          $counter=$counter+1;
>          print "Counter is now $counter.\n";
>          print "You still have a few tries left.\n";
>          }
> }
> print "You failed!  The human race is doomed!\n";

-- 
Live in a world of your own, but always welcome visitors.


More information about the Oakland mailing list