Convert MySQL functions POSITION and SUBSTR from JavaScript functions indexOf and charAt - mysql

I'd like to transform a JavaScript function to a MySQL one.
The JavaScript code is like:
var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
var s = 0;
for( i = 1; i <= 13; i += 2 )
s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
for( i = 0; i <= 14; i += 2 )
s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
So, I've "translated" it to:
SET set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
SET set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
SET setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
SET setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
SET s=0;
SET v_counter = 0;
WHILE v_counter < 14
DO
SET set1IndexOf = POSITION(SUBSTR(codFisc, v_counter+1, 1) IN set1);
SET s = s + POSITION(SUBSTR(set2, set1IndexOf, 1) IN setpari) -1;
SET v_counter=v_counter+2;
END WHILE;
SET v_counter = 0;
WHILE v_counter < 15
DO
SET set1IndexOf = POSITION(SUBSTR(codFisc, v_counter+1, 1) IN set1);
SET s = s + POSITION(SUBSTR(set2, set1IndexOf, 1) IN setdisp) -1;
SET v_counter=v_counter+2;
END WHILE;
But the MySQL function returns a wrong result.
I know that charAt and indexOf are zero-based indexes, while SUBSTR and POSITION are one-based indexes. So I've incremented the v_counter but it is still wrong.
I can't see any difference between the two codes, but there's a bug somewhere.
Anyone can help me, please?
Thanks

Related

How to vectorize a nested loops in octave?

I need to speed up my function and I am looking for an efficient way to calculate this two loops below.
Any help would be appreciated.
for i=1:n
for j=1:m
ig = i + (j-1)*n;
coord(ig,1) = i;
coord(ig,2) = j;
end
end
for j=1:m
for i=1:n-1
k = (j-1)*(n-1) + i;
conec(k,1) = (j-1)*n + i;
conec(k,2) = (j-1)*n + i+1;
C(k,1) = CH;
end
end
for k=1:nc
p = conec(k,1);
q = conec(k,2);
aux = [C(k) -C(k) ; -C(k) C(k)];
A([p q],[p q]) = A([p q],[p q]) + aux;
end

export all rows of a html table with pagenation to excel sheet

I have one html table where pagenation is used. So it displays 10 rows once. When i am exporting my table to a excel, only the first 10 rows which are there in the html table are getting exported. I want all the rows to be exported. Please suggest some way.
Below is the code I am using.
function exportToExcel(){
var mytable = document.getElementById("tlb_setup");
var rowCount = mytable.rows.length;
var colCount = mytable.getElementsByTagName("tr")[0].getElementsByTagName("th").length;
var ExcelApp = new ActiveXObject("Excel.Application");
ExcelApp.Workbooks.Open("........\\requestsInExcel.xlsx");
var ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelApp.Visible = true;
//var ExcelSheet = ExcelApp.Worksheets("Sheet1");
//ExcelSheet.Application.Visible = true;
for (var i = 0; i < rowCount; i++) {
for (var j = 0; j < colCount; j++) {
if (i == 0) {
str = mytable.getElementsByTagName("tr")[i].getElementsByTagName("th")[j].innerText;
}
else {
str = mytable.getElementsByTagName("tr")[i].getElementsByTagName("td")[j].innerText;
}
ExcelApp.ActiveSheet.Cells(i + 1, j + 1).Value = str;
//Workbook.SaveAs("........\\requestsInExcel.xlsx");
}
}
//ExcelApp.ActiveWorkbook.Save();
ExcelApp.ActiveSheet.columns.autofit;
ExcelSheet.Application.Visible = true;
DisplayAlerts = true;
}

Random hexidecimal color generator in AS3

I wanted to create the ability to have "random" (pseudo-random) colors generated and came up with this code intended to create all and any color.
I'm very new to programming and wanted to see if anyone on S.O. had any comments or criticisms, the code works great. Only problem is the colors occasionally being too similar making it difficult to differentiate bewteen them.
I know this is likely a very brute force fashion of coding but its what I thought of.
Hexidecimal generator
public class colorGenerator
{
public var color:int;
private var randomnumber:Number;
private var first:String = "";
public function colorGenerator():void
{
var colorstring:String = "0x";
var transfer:String = "0x";
for ( var i:uint = 0; i < 6; i++)
{
randomhex();
colorstring += first;
}
transfer = colorstring;
color = int(transfer);
}
public function randomhex():void
{
randomnumber = Math.random();
if ( -1 < randomnumber < ((.99 / 16) * 1))
first = "0";
else if ( ((.99/16)*1) < randomnumber < ((.99/16)*2))
first = "1";
else if ( ((.99/16)*2)< randomnumber < ((.99/16)*3))
first = "2";
else if ( ((.99/16)*3)< randomnumber < ((.99/16)*4))
first = "3";
else if ( ((.99/16)*4)< randomnumber < ((.99/16)*5))
first = "4";
else if ( ((.99/16)*5)< randomnumber < ((.99/16)*6))
first = "5";
else if ( ((.99/16)*6)< randomnumber < ((.99/16)*7))
first = "6";
else if ( ((.99/16)*7)< randomnumber < ((.99/16)*8))
first = "7";
else if ( ((.99/16)*8)< randomnumber < ((.99/16)*9))
first = "8";
else if ( ((.99/16)*9)< randomnumber < ((.99/16)*10))
first = "9";
else if ( ((.99/16)*10)< randomnumber < ((.99/16)*11))
first = "A";
else if ( ((.99/16)*11)< randomnumber < ((.99/16)*12))
first = "B";
else if ( ((.99/16)*12)< randomnumber < ((.99/16)*13))
first = "C";
else if ( ((.99/16)*13)< randomnumber < ((.99/16)*14))
first = "D";
else if ( ((.99/16)*14)< randomnumber < ((.99/16)*15))
first = "E";
else if ( ((.99/16)*15)< randomnumber < 2)
first = "F";
}
}
I then just assign the hexidecimal value to a variable in another class
var acolor:colorGenerator = new colorGenerator;
var COLOR:uint = acolor.color
Thanks for any comments!
This should work as well.
Math.random() * 0xFFFFFF;
Not tested but this should be more "random":
var red : int = Math.floor(Math.random()*255);
var green : int = Math.floor(Math.random()*255);
var blue : int = Math.floor(Math.random()*255);
var color : int = red << 16 | green << 8 | blue;

Mysql Spatial Extension. How to check if Linestring CONTAINS a Point?

I'm trying to determine if Linestring have Point.... f.e.
SET ls = geomfromtext('LINESTRING(1 0,3 0)');
SET p = geomfromtext('POINT(2 0)');
if i do CONTAINS(ls,p) i have true. but there is no point (2 0) in line
i need exactly containing. is there any function for it?
i tried all functions from documentation and non of them do what i need.
f.e.
SELECT ASTEXT(path) FROM paths WHERE INTERSECTS(path, GEOMFROMTEXT('POINT(604 0)'))
gives "wrong" results
LINESTRING(572 0,600 0,601 0,602 0,603 0,604 0,605 0,606 0,607 0,608 0,402 0)
LINESTRING(402 0,609 0,610 0,611 0,612 0,613 0,614 0,615 0,616 0,617 0,618 0,619 0,620 0,621 0,622 0,623 0)
LINESTRING(359 0,449 0,801 0,422 0,802 0,803 0,498 0)
LINESTRING(572 0,795 0,796 0,797 0,798 0,799 0,800 0,345 0,359 0)
LINESTRING(792 0,768 0,793 0,794 0,572 0)
LINESTRING(342 0,904 0,905 0,906 0)
LINESTRING(912 0,914 0,915 0,916 0,341 0)
LINESTRING(344 0,917 0,918 0,919 0,920 0,800 0)
LINESTRING(918 0,922 0,923 0,924 0,925 0,926 0,927 0,343 0)
LINESTRING(940 0,947 0,948 0,949 0,604 0)
MBRWITHIN gives the same result
i wrote a function, but it is very-very slow:
FUNCTION `IDIL`(`id` INT, `line` LINESTRING) RETURNS INT(1)
NO SQL
DETERMINISTIC
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE p1X INT(20);
DECLARE p1Y INT(20);
DECLARE p1 POINT;
DECLARE i INT DEFAULT 0;
DECLARE result INT(1) DEFAULT 0;
SET n = NUMPOINTS(line);
WHILE i<n DO
SET p1 = POINTN(line, (i+1));
SET p1X = X(p1);
SET p1Y = Y(p1);
IF p1X=id OR p1Y=id THEN RETURN 1; END IF;
SET i = i + 1;
END WHILE;
RETURN result;
END$$
Could you please check out the following reference on this article
SQLFIDDLE
SET #ls = 'LineString(1 0,3 0)';
SET #xs = geomfromtext(#ls);
SET #p = geomfromtext('POINT(2 0)');
SELECT MBRWithin(#xs,#p);
Sorry I have given the wrong link.
results
MBRWITHIN(#XS,#P)
0
Looking at your line 1 0, 3 0 --> 2 0 point exists on it.

Multiple statements in a for loop (1084 error)

I'm trying to do a binary search in a for loop. However, flash does not like the following for loop.
for(var select:int = Math.floor((min + max / 2)), var turns:int = 0; turns < input.length / 2 + 1; turns++, select= Math.floor((min + max / 2))){
if(input[select] > want){
max = select;
} else if (input[select] < want){
min = select;
} else {
return select;
}
}
On the first line I get 1084: Syntax error: expecting identifier before var. I think I know why (I'm using , to separate the different statements), but how do I fix it? ; won't work since it's what the for loop uses.
( var select:int = Math.floor((min + max / 2)) ; var turns:int = 0); turns < input.length / 2 + 1; //etc
does not work either.
Simply ditching the second var (after the comma) should work I believe will do it:
for(var select:int = Math.floor((min + max / 2)), turns:int = 0; turns < input.length / 2 + 1; turns++, select= Math.floor((min + max / 2))){
if(input[select] > want){
max = select;
} else if (input[select] < want){
min = select;
} else {
return select;
}
}
Do you have to do this as a for loop? This would work much better as a recursive function.
Here it is as a for loop. Try pulling the select variable outside the scope of the for loop.
var select: int = Math.floor((min+max/2));
for(var turns:int = 0; turns < input.length / 2 + 1; turns++){
if(input[select] > want){
max = select;
select= Math.floor((min + max / 2))
} else if (input[select] < want){
min = select;
select= Math.floor((min + max / 2))
} else {
return select;
}
}