APM: some simple questions

Mike South msouth at shodor.org
Tue Jan 27 18:22:14 CST 2004


On Tue, Jan 27, 2004 at 12:10:43PM -0600, Goldilox wrote:
> is it better form to do this:
> 
> while($exlen > 0){$indent=$indent."  ";$exlen=$exlen-1;}
> 
> or this
> 
> while($exlen){$indent=$indent."  ";$exlen=$exlen-1;}
> 
> (or is the second one even going to work?)

The first one is safer, because if $exlen had a value of 1.5 to 
start with, the second one is an endless loop.  

I agree with others who suggested the ' X ' operator for this 
situation.  However, you might want another "one line loop"
in the future, so you might look at the statement-modifer "for",
like this:


$spaces = ' ' x 2;  # this way you get to use 
                         # BOTH ' x ' operator AND 
                         # a one-line loop!  No
                         # extra charge! :)

$indent .= $spaces for 1..$exlen;

mike




More information about the Austin mailing list