using Perl range operator for html table - perl

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>

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.

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"';

Related

How to filter number using range in tcl?

set a [ 100 200 300 1677 ]
foreach s {
if { 1000 <= $s <= 2000 } {
puts "$s"
}
}
I want filter the number 1677 as output using the range 1000 to 2000 . any solutions please guide....
Your code has multiple issues:
To store a list in a variable, either use list, or put the values in braces (there are other methods too, but those are the most common):
set a [list 100 200 300 1677]
set a {100 200 300 1677}
You need to pass the list to the foreach command
foreach s $a {...}
The condition in your if statement doesn't evaluate as you might expect: 1000 <= $s results in a boolean, 0 or 1, which is always less than 2000. So the condition is always true. Instead you should use something like:
if {1000 <= $s && $s <= 2000} {...}

Perl simple way to do multiple decision such as grade scores (except if-elif)?

Try to seek Perl experts feedback on approaching this simple scores grading. What I came up is to use conditional opeartor to mimic multiway branch, but wonder if there's more simple/straight syntax to express to enhance the readability (and future maintainability).
Coming from Python/C, and pick up this new language, so I try to explore new syntax. if this sounds too naïve.
my $grade =
($score < 60) ? "F" :
($score < 68) ? "C" :
($score < 75) ? "B" :
($score < 90) ? "B+" :
($score < 95) ? "A" :
($score <= 100) ? "A+" :
"No Grade"; # catch-all default
You'd either need an hash array of 101 entries, or one with 21 and special cases. The optimization of using a hash array in this fashion would be premature without cause.
One would often use a hash or array as a dispatch table in similar situations, but it doesn't work here since you'd have to include all possible values.
Well, maybe not all possible value. For example, we could truncate the numbers to remove decimal points. And if all the value were divisible by 5, we divide by 5 to obtain a manageable 21 values. Then a dispatch table could be used. (Though an array would work better than a hash.)
That 68 means we can't do that. Either 65, 66 and 67, or 68 and 69 would need to be special-cased. The 100 is also a special value since you apparently want to tolerate invalid inputs. And that suggests a bad design unless performance is crucial.
Let's say the optimization was warranted.
You could setup the following tables:
my #grades_lkup;
$grades_lkup[ $_ ] = "F" for 0 .. 59;
$grades_lkup[ $_ ] = "C" for 60 .. 67;
$grades_lkup[ $_ ] = "B" for 68 .. 74;
$grades_lkup[ $_ ] = "B+" for 75 .. 89;
$grades_lkup[ $_ ] = "A" for 90 .. 94;
$grades_lkup[ $_ ] = "A+" for 95 .. 100;
Then all you'd need is
my $grade = $grades_lkup[ $score ] // "No Grade";

How can I remove <math></math> multiline sections with Perl?

How can I remove multiline sections with Perl?
I have such wiki test code:
{|
|-
| colspan="2"|
: <math>
[\underbrace{\color{Red}4,2}_{4 > 2},5,1,7] \rightarrow
[2,\underbrace{\color{OliveGreen}4,5}_{4 < 5},1,7] \rightarrow
[2,4,\underbrace{\color{Red}5,1}_{5 > 1},7] \rightarrow
[2,4,1,\underbrace{\color{OliveGreen}5,7}_{5 < 7}]
</math>
|-
|
: <math>
[\underbrace{\color{OliveGreen}2,4}_{2 < 4},1,5,{\color{Blue}7}] \rightarrow
[2,\underbrace{\color{Red}4,1}_{4 > 1},5,{\color{Blue}7}] \rightarrow
[2,1,\underbrace{\color{OliveGreen}4,5}_{4 < 5},{\color{Blue}7}]
</math>
: <math>
[\underbrace{\color{Red}2,1}_{2 > 1},4,{\color{Blue}5},{\color{Blue}7}] \rightarrow
[1,\underbrace{\color{OliveGreen}2,4}_{2 < 4},{\color{Blue}5},{\color{Blue}7}]
</math>
: <math>
[\underbrace{\color{OliveGreen}1,2}_{1 < 2},{\color{Blue}4},{\color{Blue}5},{\color{Blue}7}]
</math>
|}
And I want to remove from this code all how to do it? I have done such code:
cat math-text.txt | perl -e 'while(<>) { s/<math>.+?<\/math>//gs; print $_; }'
It is not works but should since documentation explains that . will much new lines. How to do it?
The following is a python script which I use to extract all the mathematical formula from wikipedia dumps. Rather than using a multi-line regexp it scans for occurrences of <math> </math> and uses the position on the line to work out where the actual position on the line is and uses a finite state machine to find the actual equations, basically with two states determined by inEqn. It does a few other things like find the title and name space and attributes in the maths tags.
As dumps are in the order of 100MB using a line by line approach may well end up being more efficient than multi-line regexps.
import sys
import re
titleRE = re.compile('<title>(.*)</title>')
nsRE = re.compile('<ns>(.*)</ns>')
mathRE = re.compile('</?math(.*?)>')
pageEndRE = re.compile('</page>')
title =""
attr = ""
ns = -1
inEqn = 0
for line in sys.stdin:
m = titleRE.search(line)
if m :
title = m.group(1)
expression = ""
inEqn = 0
m = nsRE.search(line)
if m :
ns = m.group(1)
start = 0
pos = 0
m = mathRE.search(line,pos)
while m :
if m.group().startswith('<math'):
attr = m.group(1)
start = m.end()
pos = start
expression = ""
inEqn = 1
if m.group() == '</math>' :
end = m.start()
expression = ' '.join([expression,line[start:end]])
print title,'\t',attr,'\t',expression.lstrip().replace('<','<').replace('>','>').replace('&','&')
pos = m.end()
expression = ""
start = 0
inEqn = 0
m = mathRE.search(line,pos)
if start > 0 :
expression = line[start:].rstrip()
elif inEqn :
expression = ' '.join([expression,line.rstrip()])
Another option might be to consider an xml parser. A SAX or DOM based parser would be able to find the equations. This might be worth considering if you want to do more sophisticated analysis of the wiki-text.

how to disable special characters text field in sugarcrm?

can anyone help me in this issue.
I want to avoid special characters(!##$%^&*()) in text field.
Actually I have added javascript code for this.
now I cant enter the spl characters. it's working perfect.
But the problem is, the data is not going to be inserted after submitted.
<script language="Javascript" type="text/javascript">
function alpha(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32 || (k >= 48 && k <= 57));
}
</script>
'customCode' => '<input type="text" onkeypress="return alpha(event)">',
You have missed the name of textfield in customCode. Your customCode should be like:
'customCode' => '< input type="text" onkeypress="return alpha(event)" name="your-textbox-name">',

Consistent random colour highlights

In a table I have columns with to and from dates, I highlight overlaps between rows taking into account the periods, this is done exhaustively in nested loops. This is not the issue.
I need the same colour for the rows that overlap.
sub highlight_overlaps {
my $date_from1;
my $date_to1;
my $date_from2;
my $date_to2;
my $i = 0;
my $j = 0;
for ($i; $i < $#DATE_HOLDER; $i++) {
$date_from1 = $DATE_HOLDER[$i][0];
$date_to1 = $DATE_HOLDER[$i][1];
my $red = int(rand(65)) + 190;
my $green = int(rand(290)) - 55;
my $blue = int(rand(290)) - 55;
for ($j=$i+1; $j<=$#DATE_HOLDER; $j++) {
$date_from2 = $DATE_HOLDER[$j][0];
$date_to2 = $DATE_HOLDER[$j][1];
if (($date_from1 le $date_to2 && $date_to1 ge $date_to2) ||
($date_from1 le $date_from2 && $date_to1 le $date_to2) ||
($date_from1 gt $date_from2 && $date_from1 lt $date_to2)) {
$tb->setCellStyle($i+2, 6, "background-color:rgb($red,$green,$blue);font-size:9pt");
$tb->setCellStyle($i+2, 7, "background-color:rgb($red,$green,$blue);font-size:9pt");
$tb->setCellStyle($j+2, 6, "background-color:rgb($red,$green,$blue);font-size:9pt");
$tb->setCellStyle($j+2, 7, "background-color:rgb($red,$green,$blue);font-size:9pt");
}
}
}
}
This works fine if it's just a pair of dates; say:
1) 25-06-2012 27-06-2012
2) 18-06-2012 29-06-2012
Will get the same colour
If though I have
0) 26-06-2012 28-06-2012
1) 25-06-2012 27-06-2012
2) 18-06-2012 29-06-2012
0 will get a different colour while 1 & 2 are paired as intended.
When and how to pick colours so that different colours are only applied to different overlaps?
Following up on the first answer; how may I represent overlaps in order to store them in a data structure, so that I can colour them after their detection?
You'll have to compare each interval against each other interval, and put them in 'buckets' when they are equal. Now when you compare an interval to a third interval, you put the third in the same bucket as the interval.
Then you print the buckets.
Perl's hash would make for fine buckets.
About your overlap detection
There is no overlap if
date1_to < date2_from OR
date2_to < date1_from
Or, in Perl:
if ($date_to1 lt $date_from2 || $date_to2 lt $date_from1) {
#overlap
}
Invert that either using Perl's unless, or using de Morgan:
if ($date_to1 ge $date_from2 && $date_to2 ge $date_from1) {
#overlap
}