1

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?

2
  • Why does critical have the class info? Commented Oct 13, 2016 at 13:43
  • @simbabque it's just class for the color, i will change it after, in fact i have multiple tables to fill with those value, it's just a way to make it work in the first place ^^ Commented Oct 13, 2016 at 14:33

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.