Hari Krishnaan wrote at Thu, 10 Jul 2003 14:03:32 -0700:

> I was using a for loop in the following manner in one of my perl programs.
>  
> for($j=31, $n=$initial;$j>=0,$n<=$final;$j--,$n++) {
>  
> # Executing statements here
> }
> }
> 1) Is it legal in perl to use the for loop as mentioned above ?

Yes (taking care of what Jenda already told),
but it is not very Perlish.

An independent reader (might be yourself in a year)
can't understand on the first glance what the purpose of that loop is.

I suggest that you want to run $n over $initial..$start,
but at most 32 times.
A, IMHO more readable solution might be:


{
    my $loops = 0;
    foreach my $n ($initial .. $final) {
        last if ++$loops > 32;
        # ... your stuff
    }
}

or perhaps shorter and also good readable:

foreach my $n ( ($initial .. $final)[0 .. 31] ) {
    # ... your stuff
}

But of course, that's also a question of personal style :-)


Greetings,
Janek

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to