in reply to Nested While Loops, foreach and next

Your code is rather unreadable due to poor indenting.

Your post includes far more code than necessary to demonstrate the problem.

Your post includes too little to demonstrate the problem. You're saying the problem is that a call to log is being reached when it shouldn't, yet there is no such function call in your code.

You've made it impossible for us to help you because Perl does indeed behave as you think it should.

When next is called in

for (...) { ... &conflict_check; ... log ... } sub conflict_check { ... if (...) { next; } ... }
Perl does indeed skip the to the next pass of the loop directly (skipping log), although it does issue a warning* because the following would be clearer
for (...) { ... next if &conflict_exists; ... log ... } sub conflict_exists { ... if (...) { return 1; } ... return 0; }

* — Please tell me you are using use strict; use warnings;.

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

    Thanks for the reply.

    I have made the recommended changes to &conflict_check and the innermost foreach but the behaviour is the same. The best way I could describe it is that it is treating the next as a last.

    I always use use strict and warnings - it actually makes it easier because the errors are more helpful for fixing things before they have a confusing knock-on effect for other lines.

    All of the lines I have included are wrapped by PerlMonks - it's much clearer without this. There are several logs in the script, they are part of an equation a couple of thousand lines long with 185 variables. I know this code works because they are working fine as part of a manual process where frequency, antenna length and channel width are selected by the user. The only part that doesn't work properly is this while/foreach nested loop which doesn't cycle through antenna lengths as expected.

    I have included below all lines that I could find with log (in no particular order) and the log subroutine. All of the log lines are either part of &calculate_rsl;, &calculate_oav_h or &calculate_oav_v; - all of which shouldn't be reached if certain antenna lengths match incompatible frequencies as defined in &conflict_check;.

    $FREE_SPACE_PATH_LOSS = 92.45+20*log10($PATH_LENGTH)+20*log10($FREQUEN +CY); $OUTAGE_FACTOR_H = 0.139735174-0.172*log10($FADE_MARGIN/$PATH_ATTENUAT +ION_H); $GEOCLIMATIC_CONVERSION_FACTOR = (10.3-5*log10(1-abs(cos($LATITUDE))** +0.7)-2.8*log10($PATH_LENGTH)+1.8*log10(abs($INCLINATION))); $OUTAGE_FACTOR_V = 0.139735174-0.172*log10($FADE_MARGIN/$PATH_ATTENUAT +ION_V); sub log10 { my $n = shift; return log($n)/log(10); }

    Thanks for any additional help that anybody can offer :-)

    Kind regards,

    Ben

      foreach ( ($SITE_B_ANTENNA_LENGTH) = @SITE_B_ANTENNA_LENGTH_QUERY = $S +ITE_B_ANTENNA_LENGTH_QUERY->fetchrow_array() )
      looks really weird. In fact, I have no idea what you are trying to accomplish. Maybe you want
      foreach my $SITE_B_ANTENNA_LENGTH ( $SITE_B_ANTENNA_LENGTH_QUERY->fetc +hrow_array() )
      or
      my @SITE_B_ANTENNA_LENGTHS = $SITE_B_ANTENNA_LENGTH_QUERY->fetchrow_ar +ray(); foreach my $SITE_B_ANTENNA_LENGTH (@SITE_B_ANTENNA_LENGTHS)