Home > OS >  using Perl range operator for html table
using Perl range operator for html table

Time:07-20

I'm having issues with the Perl range operator when assigning a range to my variable.

I have an HTML table that outputs numbers from a SQL query. How can I add a range from 98-99 to color code the data yellow?

I have this working for other cases (<100 and >100) but can't seem to get the 3rd option to work. Thank you in advance for helping!!!

my $highlightProjMoves3300red = ($variableProjMoves3300 < 100) ? '  STYLE="COLOR:RED"' : '';
my $highlightProjMoves3300green = ($variableProjMoves3300 > 100) ? '  STYLE="COLOR:GREEN"' : '';
my $highlightProjMoves3300yellow = ($variableProjMoves3300 = (98..99)) ? '  STYLE="COLOR:YELLOW"' : '';

<tr>
    <td>GATE</td>
    <td>3300</td>
    <td>$variable3300</td>
    <td>$variableYTRG3300</td>
    <td $highlight3300>$variableYESTDELTA3300</td>
    <td>$variableDTDMOVES3300</td>
    <td $highlightProjMoves3300red $highlightProjMoves3300green $highlightProjMoves3300yellow >$variableProjMoves3300%</td>
    <td>$variablePASTWEEKMOVES3300</td>
    <td></td>
    <td></td>
    <td></td>
    <td>$variableCURRENT_WIP_GATE</td>
    
</tr>

CodePudding user response:

Your approach is failing because the range operator generates a list of values. Comparing a variable to a list doesn't give you a true result if the list contains the value.

You'd need to search for it.

#!/usr/bin/perl

use v5.20;
use warnings;
use List::MoreUtils qw(any);

my @values = (50, 98, 200);
foreach my $variableProjMoves3300 (@values) {
    if (any {$_ == $variableProjMoves3300} (98..99)) {
        say "$variableProjMoves3300 is between 98 and 99 (inclusive)";
    }
}

This isn't an approach I'd recommend though, at least not for dealing with the case "between two numbers".

A traditional

 if (98 <= $variableProjMoves3300 && $variableProjMoves3300 <= 99) {

… is clearer and (I expect) faster.

CodePudding user response:

98 <= $variableProjMoves3300 <= 99   # 5.32 

or

98 <= $variableProjMoves3300 && $variableProjMoves3300 <= 99

That said, having a collection of variables make no sense. You want to set the style based on the number of moves, so you need one variable for the style.

my $highlightProjMoves3300Style = 
     $variableProjMoves3300 <   98 ? ' STYLE="COLOR:RED"'
   : $variableProjMoves3300 <  100 ? ' STYLE="COLOR:YELLOW"'
   : $variableProjMoves3300 == 100 ? ''
   :                                 ' STYLE="COLOR:GREEN"';

Or if you didn't mean to skip 100,

my $highlightProjMoves3300Style = 
     $variableProjMoves3300 <   98 ? ' STYLE="COLOR:RED"'
   : $variableProjMoves3300 <  100 ? ' STYLE="COLOR:YELLOW"'
   :                                 ' STYLE="COLOR:GREEN"';
  •  Tags:  
  • perl
  • Related