CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Nested While Loops, foreach and next: Edit Log



_Chipwiz_Ben_
Novice

Jun 1, 2011, 9:11 AM


Views: 1971
Nested While Loops, foreach and next

I'm trying to build a script that checks link availability and licence costs for given frequencies, channel widths and antenna lengths. Some antenna lengths are not compatible with certain frequencies. The purpose of this section of code is to compare all frequency, channel width and antenna length combination and calculate the availability but not running the subroutines for availability and price calculations if the frequency does not match the antenna length.


Code
$FREQUENCY_QUERY = $dbh->prepare("SELECT `id`, `frequencies` 
FROM `microwave-frequencies`
ORDER BY `frequencies` DESC");

$CHANNEL_QUERY = $dbh->prepare("SELECT `channel`
FROM `microwave-modulation-lookup`
WHERE `rate` = '$DATA_RATE'");

$SITE_A_ANTENNA_LENGTH_QUERY = $dbh->prepare("SELECT `length`
FROM `microwave-antenna-length-lookup`");

$SITE_B_ANTENNA_LENGTH_QUERY = $dbh->prepare("SELECT `length`
FROM `microwave-antenna-length-lookup`");

$FREQUENCY_QUERY->execute( );
while ( ($FREQUENCY_ID) = @FREQUENCY_QUERY = $FREQUENCY_QUERY->fetchrow_array() )
{

$FREQUENCY_ID = "$FREQUENCY_QUERY[0]";
$FREQUENCY = "$FREQUENCY_QUERY[1]";


$CHANNEL_QUERY->execute( );
while ( ($CHANNEL_SIZE) = @CHANNEL_QUERY = $CHANNEL_QUERY->fetchrow_array() )
{

$CHANNEL_SIZE = "$CHANNEL_QUERY[0]";


$SITE_A_ANTENNA_LENGTH_QUERY->execute( );
foreach ( ($SITE_A_ANTENNA_LENGTH) = @SITE_A_ANTENNA_LENGTH_QUERY = $SITE_A_ANTENNA_LENGTH_QUERY->fetchrow_array() )
{

$SITE_A_ANTENNA_LENGTH = "$SITE_A_ANTENNA_LENGTH_QUERY[0]";


$SITE_B_ANTENNA_LENGTH_QUERY->execute( );
foreach ( ($SITE_B_ANTENNA_LENGTH) = @SITE_B_ANTENNA_LENGTH_QUERY = $SITE_B_ANTENNA_LENGTH_QUERY->fetchrow_array() )
{

$SITE_B_ANTENNA_LENGTH = "$SITE_B_ANTENNA_LENGTH_QUERY[0]";

&conflict_check;
&calculate_rsl;
&calculate_oav_h;
&calculate_oav_v;
&calculate_licence_fee;

$tb->addRow( "$TABLE_ROW_COUNT", "$DATA_RATE Mbps", "$FREQUENCY GHz", "$CHANNEL_SIZE MHz", "$SITE_A_ANTENNA_LENGTH", "$SITE_B_ANTENNA_LENGTH", "$OVERALL_AVAILABILITY_H_RESULT %", "$OVERALL_AVAILABILITY_V_RESULT %", "&pound;$ANNUAL_LICENCE_FEE", "<a href='ofcom-document-generator.cgi?LINKDETAILS=True&FREQUENCY=$FREQUENCY&CHANNEL_SIZE=$CHANNEL_SIZE&SITE_A_ANTENNA_LENGTH=$SITE_A_ANTENNA_LENGTH&SITE_B_ANTENNA_LENGTH=$SITE_B_ANTENNA_LENGTH'>Link Details</a>" );
$TABLE_ROW_COUNT++;

if ($LINK_TYPE eq 'Core Infrastructure') {
if ($OVERALL_AVAILABILITY_H_RESULT >= 99.99) {
$tb->setCellClass ($TABLE_ROW_COUNT, 7, 'tbrowgreen');
}
elsif ($OVERALL_AVAILABILITY_H_RESULT < 99.99) {
$tb->setCellClass ($TABLE_ROW_COUNT, 7, 'tbrowerror');
}

if ($OVERALL_AVAILABILITY_V_RESULT >= 99.99) {
$tb->setCellClass ($TABLE_ROW_COUNT, 8, 'tbrowgreen');
}
elsif ($OVERALL_AVAILABILITY_V_RESULT < 99.99) {
$tb->setCellClass ($TABLE_ROW_COUNT, 8, 'tbrowerror');
}
}
elsif ($LINK_TYPE eq 'Customer Link') {
if ($OVERALL_AVAILABILITY_H_RESULT >= 99.95) {
$tb->setCellClass ($TABLE_ROW_COUNT, 7, 'tbrowgreen');
}
elsif ($OVERALL_AVAILABILITY_H_RESULT < 99.95) {
$tb->setCellClass ($TABLE_ROW_COUNT, 7, 'tbrowerror');
}

if ($OVERALL_AVAILABILITY_V_RESULT >= 99.95) {
$tb->setCellClass ($TABLE_ROW_COUNT, 8, 'tbrowgreen');
}
elsif ($OVERALL_AVAILABILITY_V_RESULT < 99.95) {
$tb->setCellClass ($TABLE_ROW_COUNT, 8, 'tbrowerror');
}
}
}
}
}
}


For some reason, the two inner most foreach's, if I change them to whiles, seem to ignore the &conflict_check; next statement and throw up a logarithmic error due to the fact that the frequency does not work with the antenna length (Can't take log of -0.364299 at /var/www/dev/ofcom-document-generator.cgi line 3358.). A little example of the &conflict_check; :


Code
sub conflict_check { 
if (($FREQUENCY eq '7') && ($SITE_A_ANTENNA_LENGTH eq '0.3')) {
next;
}
if (($FREQUENCY eq '7') && ($SITE_B_ANTENNA_LENGTH eq '0.3')) {
next;
}
if (($FREQUENCY eq '11') && ($SITE_A_ANTENNA_LENGTH eq '0.3')) {
next;
}
if (($FREQUENCY eq '11') && ($SITE_B_ANTENNA_LENGTH eq '0.3')) {
next;
}
if (($FREQUENCY eq '28') && ($SITE_A_ANTENNA_LENGTH eq '0.8')) {
next;
}
if (($FREQUENCY eq '28') && ($SITE_B_ANTENNA_LENGTH eq '0.8')) {
next;
}
...and so on...


It is of my understanding that the next; should exit the current while (or foreach) and so should not process any of the other subroutine calls, therefore should not attempt to `log` the number which is usually a minus.

The two inner most foreach's do work if they are foreach's and not while's as earlier noted, however they seem to exit after just one loop so the only antenna length that gets calculated is 0.3. Possible lengths are 0.3, 0.6, 0.8, 1.0 and 1.2 for both site A and site B.

Frequency SQL query outputs:
7, 11, 13, 15, 18, 23, 26, 28, 38

Channel Width SQL query outputs:
7, 14, 28, 56

I have put all 4 SQL queries into Perl arrays and referenced them that way, but it has not changed anything, they behave in the same way as with SQL query outputs.

Many thanks to anyone who can shed any light onto the situation.

Kind regards,
Ben


(This post was edited by _Chipwiz_Ben_ on Jun 1, 2011, 9:18 AM)


Edit Log:
Post edited by _Chipwiz_Ben_ (Novice) on Jun 1, 2011, 9:18 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives