in reply to Nested While Loops, foreach and next

As ikegami commented, I also found the indenting style to hamper understanding. This is more than line wrap, which is actually fine. It is a matter of what you have lined up with what. Example:
$FREQUENCY_QUERY->execute( ); while ( ($FREQUENCY_ID) = @FREQUENCY_QUERY = $FREQUENCY_QUERY- +>fetchrow_array() ) { $FREQUENCY_ID = "$FREQUENCY_QUERY[0]"; $FREQUENCY = "$FREQUENCY_QUERY[1]"; $CHANNEL_QUERY->execute( ); ### better ### $FREQUENCY_QUERY->execute( ); while ( ($FREQUENCY_ID) = @FREQUENCY_QUERY = $FREQUENCY_QUERY->fetchro +w_array() ) { $FREQUENCY_ID = "$FREQUENCY_QUERY[0]"; $FREQUENCY = "$FREQUENCY_QUERY[1]"; $CHANNEL_QUERY->execute( ); .... }
The studies that I've read say that the number of characters of indentation between levels should be either 3 or 4, 2 is too few and 5 or more is too many. In practice, I have found this to be good advice.

You claim that use strict; and use warnings; are being used. In the case of strictures, one of the purposes is to limit the visibility of variables to just where you need it. If there is some huge block that pre-declares things like $FREQUENCY_ID and gives it scope that makes it visible in all other code, it defeats the whole purpose.

I would expect to see code like this:

$FREQUENCY_QUERY->execute( ); while ( my ($FREQUENCY_ID, $FREQUENCY) = $FREQUENCY_QUERY->fetchrow_ar +ray() ) { $CHANNEL_QUERY->execute( ); .... } ####### #note # $FREQUENCY_ID = "$FREQUENCY_QUERY[0]"; #not needed, delete # $FREQUENCY = "$FREQUENCY_QUERY[1]"; #not needed, delete #
I don't think that your foreach() loops do what you want. If you replace "foreach" with "while", what happens will be very different, so it is not clear at all to me what you are saying works and what doesn't. See if you can replicate the problem with a much more simple example.

Replies are listed 'Best First'.
Re^2: Nested While Loops, foreach and next
by Chipwiz_Ben (Initiate) on Jun 03, 2011 at 10:54 UTC

    Thanks for your help guys.

    Ben