Phoenix.pm: Please help, my brain is fried

Kevin Buettner kev at primenet.com
Wed Jan 26 21:08:18 CST 2000


On Jan 26,  7:07pm, Scott Walters wrote:

> > 
> >   sub get_char(){
> >       srand;
> >       $count++;
> > 
> >       my $char = int(rand(57))+65;
> >       print $count, "  ", chr($char), "\n";
> > 
> >       get_char() if ($char >= 91 && $char <= 96);
>         $char=get_char() while($char >= 91 && $char <=96);        
> >       return $char;
> >   }
> > 
> 
> Sorry, I'm one of those people who edits, saves, realizes he wasnt
> thinking, edits again, saves, then runs the code...
> Yes, $char does go out of scope, and there is a respectable chance each
> that you will get two values, both of which are out of range, in a row,
> in which case you need a while, not an if.

The use of "while" in this case is unnecessary.  "if" is sufficient
because the recursion handles the case of two or more successive out
of range characters.

Note that if your programming language supports recursive functions,
then it is possible to write all of your code without the use of
"while" or other looping constructs.  (You get very good at this
sort of thing if you program in a (pure) functional language for a
while.)

Kevin

-- 
Kevin Buettner
kev at primenet.com, kevinb at redhat.com



More information about the Phoenix-pm mailing list