htmlperlcgi

Print HTML Table from an array with condition with Perl CGI


I have made a script that returns me an array with several lines like:

DATA:VALUE:VALUE_MAX

I need to fill a table with those value like:

NAME |  Status
--------------------------
DATA |  OK/minor/warning...
.... |  .........
.... |  .........

with VALUE and VALUE_MAX I calculate the percent which gives me the status.

here is my code for print the table:

my @i = my_status();

print <<END;
<div class="container">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
END
my $inc = 0;
while (@i) {
my @temp = split /:/, @i[$inc];
my $name = $temp[0];
my $percent = ($temp[1] * $temp[2] / 100);
my $status = undef;
if ($percent <= 24 ) {
print "<tr class='info'>";
$status = "Critical !";
}
elsif ($percent <= 49 ) {
print "<tr class='danger'>";
$status = "Danger !";
}
elsif ($percent <= 74 ) {
print "<tr class='warning'>";
$status = "Warning";
}
elsif ($percent <= 99 ) {
print "<tr class='active'>";
$status = "Minor";
}
elsif ($percent == 100 ) {
print "<tr class='success'>";
$status = "OK";
}
print "<td>$name</td>";
print "<td>$status</td>";
print "</tr>";
$inc++;
}
print <<END;
</tbody>
</table>
</div>
END

My script "my_status" is a bit long to execute, it's full of server request...

but the thing is, on the HTML page, everything is a mess, I get wrong value, and an endless loop who print only "Critical !" in status columns

What is wrong with my script?


Solution

  • You are not iterating @i in your while loop. Your line

    while (@i) {
    

    means that it will stay in the loop as long as @i is true. Because that's an array, that means that as long as there are items in @i, it will stay in the loop.

    You do not remove anything from @i inside of the loop. There are no shift or pop commands, and you also do not overwrite @i. So it will stay indefinitely. You've got yourself an infinite loop.


    What you want instead is probably a foreach loop. Then you also don't need $inc. It will put each element inside of @i into $elem and run the loop.

    foreach my $elem (@i) {
        my @temp    = split /:/, $elem;
        my $name    = $temp[0];
        my $percent = ( $temp[1] * $temp[2] / 100 );
        my $status  = undef;
        if ( $percent <= 24 ) {
            print "<tr class='info'>";
            $status = "Critical !";
        }
        elsif ( $percent <= 49 ) {
            print "<tr class='danger'>";
            $status = "Danger !";
        }
        elsif ( $percent <= 74 ) {
            print "<tr class='warning'>";
            $status = "Warning";
        }
        elsif ( $percent <= 99 ) {
            print "<tr class='active'>";
            $status = "Minor";
        }
        elsif ( $percent == 100 ) {
            print "<tr class='success'>";
            $status = "OK";
        }
        print "<td>$name</td>";
        print "<td>$status</td>";
        print "</tr>";
    }
    

    You can read up on loops in perlsyn starting from for loops.