table.sort throws " invalid order function" - sorting

I'm developing a simple friend system and want to sort the friendData with some
rules.
I compared two friends' status , level and offline-Time.
PS:A friend has 3 status.(Online = 3,Busy = 2,Offline = 1).
Here's my code.
local function compare(friend1,friend2)
local iScore1 = 0
local iScore2 = 0
if friend1["eStatus"] > friend2["eStatus"] then
iScore1 = iScore1 + 1
end
if friend1["iLevel"] > friend2["iLevel"] then
iScore1 = iScore1 + 1
end
if friend1["iOfflineTime"] < friend2["iOfflineTime"] then
iScore1 = iScore1 + 1
end
return iScore1 > iScore2
end
table.sort(FriendData,compare)
It works when I add several friends.But when I get more friends,It throws exception "invalid order function for sorting".
Can someone please tell me how to fix it? :)

Thanks to #Paul Hebert and #Egor Skriptunoff ,I Figure it out.
The key is that compare(a,b) and compare(b,a) should have different returned results.
That means:
When iScore1 == iScore2,there should be an unique value for comparing(e.g.,accountID).
Different compared value should have different scores.
Here is the new code.
local function compare(friend1,friend2)
local iScore1 = 0
local iScore2 = 0
if friend1["eStatus"] > friend2["eStatus"] then
iScore1 = iScore1 + 100
elseif friend1["eStatus"] < friend2["eStatus"] then
iScore2 = iScore2 + 100
end
if friend1["iLevel"] > friend2["iLevel"] then
iScore1 = iScore1 + 10
elseif friend1["iLevel"] < friend2["iLevel"] then
iScore2 = iScore2 + 10
end
if friend1["iOfflineTime"] < friend2["iOfflineTime"] then
iScore1 = iScore1 + 1
elseif friend1["iOfflineTime"] > friend2["iOfflineTime"] then
iScore2 = iScore2 + 1
end
if iScore1 == iScore2 then --They are both 0.
return friend1["accountID"] > friend2["accountID"]
end
return iScore1 > iScore2
end
table.sort(FriendData,compare)

Related

I want to search similarities in Two-Dimensional array

I have one for normal array, I would like help to see how I can use it to search in a Two-Dimensional one, or see if someone can teach me a better way
I'm been really burnout lately so i would like to know if i post it in the wrong place or if I've done something wrong, please
Do
BANDERA = 0
For i = 0 To 4 - 2
If A(i) > A(i + 1) Then
T = A(i)
A(i) = A(i + 1)
A(i + 1) = T
BANDERA = 1
End If
Next
Loop Until BANDERA = 0
Do
BANDERA = 0
For i = 0 To 4 - 2
If B(i) > B(i + 1) Then
T = B(i)
B(i) = B(i + 1)
B(i + 1) = T
BANDERA = 1
End If
Next
Loop Until BANDERA = 0
c = 0
For i = 0 To 4 - 1
If B(i) <> B(i + 1) Then
c = c + 1
NUM(c) = B(i)
End If
Next
X = 0
For j = 1 To c
BANDERA = 0
For i = 0 To 4 - 1
If NUM(j) = A(i) Then
If BANDERA = 0 Then
X = X + 1
COINCI(X) = A(i)
BANDERA = 1
End If
End If
Next
Next

Output result to a text file in vbs [duplicate]

This question already exists:
Output vbs result to text file [closed]
Closed last year.
Please help to correct my code of which I need to get the result to show on screen and record it to a text file.
Set WshShell = CreateObject("WScript.Shell")
MsgBox ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))
WScript.Echo "ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))" > C:\WindowsKey.txt
Function ConvertToKey(Key)
Const KeyOffset = 52
i = 28
Chars = "BCDFGHJKMPQRTVWXY2346789"
Do
Cur = 0
x = 14
Do
Cur = Cur * 256
Cur = Key(x + KeyOffset) + Cur
Key(x + KeyOffset) = (Cur \ 24) And 255
Cur = Cur Mod 24
x = x -1
Loop While x >= 0
i = i -1
KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
If (((29 - i) Mod 6) = 0) And (i <> -1) Then
i = i -1
KeyOutput = "-" & KeyOutput
End If
Loop While i >= 0
ConvertToKey = KeyOutput
End Function
Here's your script with corrections. Note the elimination of the MsgBox line, the correction of the WScript.Echo line (with the elimination of the non-VBScript redirection code) and the addition of proper indentation:
Set WshShell = CreateObject("WScript.Shell")
WScript.Echo ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))
Function ConvertToKey(Key)
Const KeyOffset = 52
i = 28
Chars = "BCDFGHJKMPQRTVWXY2346789"
Do
Cur = 0
x = 14
Do
Cur = Cur * 256
Cur = Key(x + KeyOffset) + Cur
Key(x + KeyOffset) = (Cur \ 24) And 255
Cur = Cur Mod 24
x = x -1
Loop While x >= 0
i = i -1
KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
If (((29 - i) Mod 6) = 0) And (i <> -1) Then
i = i -1
KeyOutput = "-" & KeyOutput
End If
Loop While i >= 0
ConvertToKey = KeyOutput
End Function
Now, instead of double-clicking the script, open a Cmd prompt and enter this command (change the script name to match your vbs file):
cscript getkey.vbs>%temp%\WindowsKey.txt
The redirection is a Cmd shell thing, not a VBScript thing.
Also note that the above saves the file to your Temp folder. You were trying to save to a file on the root of C:. That will result in an accessed denied error.
Alternatively, you can change the script to write directly to a file like this:
Set WshShell = CreateObject("WScript.Shell")
Key = ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))
Const ForWriting = 2
Set oFSO = CreateObject("Scripting.FileSystemObject")
Temp = WshShell.ExpandEnvironmentStrings("%Temp%")
Set oFile = oFSO.OpenTextFile(Temp & "\WindowsKey.txt",ForWriting,True)
oFile.Write Key
oFile.Close
WScript.Echo Key
Function ConvertToKey(Key)
Const KeyOffset = 52
i = 28
Chars = "BCDFGHJKMPQRTVWXY2346789"
Do
Cur = 0
x = 14
Do
Cur = Cur * 256
Cur = Key(x + KeyOffset) + Cur
Key(x + KeyOffset) = (Cur \ 24) And 255
Cur = Cur Mod 24
x = x -1
Loop While x >= 0
i = i -1
KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
If (((29 - i) Mod 6) = 0) And (i <> -1) Then
i = i -1
KeyOutput = "-" & KeyOutput
End If
Loop While i >= 0
ConvertToKey = KeyOutput
End Function
None of this is new and it's all covered in numerous other answers, some of which you were already pointed to. No doubt this question will be closed as a duplicate, but it seemed you needed a little extra help, so here it is.

Convert to Laravel Query

I have query for query data. That is document.
"SELECT
zk_z_hako * CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
+ zk_z_bara
- ifnull(
sum(
ns_hako
* CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END
+ ns_bara
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END )
,0 ) AS TOTAL_BARA
FROM t_table1
LEFT JOIN t_table2
ON ns_kno = zk_kno
AND ns_show_flg = 0
AND ns_ymd > 'Date param'
WHERE zk_kno = Value param;
So I am not a master of Laravel. Now I need to convert this query for work with laravel. Anyone can help me?
And i have to try this query.
$squery = 'zk_z_hako * CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
+ zk_z_bara
ifnull(
sum(
ns_hako
* CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END
+ ns_bara
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END )
,0 ) AS TOTAL_BARA ';
$param1= '20160310';
$param2= '1972640100';
$results = DB::table('table1')
->select($squery)
->leftJoin('table2', function($join) use ($param1)
{
$join->on('table1.ns_kno', '=', 'table2.zk_kno');
$join->on('table1.ns_show_flg', '=', DB::raw(0));
$join->on('ns_ymd','>',DB::raw("'".$param1."'"));
})
->where('zk_kno', DB::raw($param2))
->toSql()
But it's return sql
"select `zk_z_hako` as `CASE` from `t_zaikmst` left join `t_nsyutrn` on `t_nsyutrn`.`ns_kno` = `t_zaikmst`.`zk_kno` and `t_nsyutrn`.`ns_show_flg` = 0 and `ns_ymd` > '20160310' where `zk_kno` = 1972640100"
i don't sure it true.
If you want to make custom select than you need to use raw queries as select parameter like this:
->select(\DB::raw($squery))

All these projectiles going in 1 direction

Guys so basicaly i have this pieces of code in different locations(didnt want to post all code here) in Lua . I want to make a game on love2d. and my problem is: my projectiles are always going in 1 direction. i even make projectile.animnumber but still it gets to go in 1 direction. is there any way to split this massive. (I'm a newbie so don't flame me too much)
projectile = {}
projectile.width = 30
projectile.height = 32
projectile.animNumber = 1
function love.keyreleased(key)
if (key == "space") then
shoot()
love.audio.play(magic_shotSND)
if player.animNumber == 1 then
projectile.animNumber = 1
elseif player.animNumber == 2 then
projectile.animNumber = 2
elseif player.animNumber == 3 then
projectile.animNumber = 3
else
projectile.animNumber = 4
end
end
end
-----
for i,v in ipairs(player.shots) do
if projectile.animNumber == 1 then
v.x = v.x + 300 * dt
elseif projectile.animNumber == 2 then
v.x = v.x - 300 * dt
elseif projectile.animNumber == 3 then
v.y = v.y + 300 * dt
else
v.y = v.y - 300 * dt
end
end
----
function shoot()
local shot = {}
shot.x = player.x - 16
shot.y = player.y - 8
table.insert(player.shots, shot)
end
for i,v in ipairs(player.shots) do
love.graphics.draw(skull, v.x, v.y)
end
Replace all of your "projectile.animNumber" to "v.animNumber"
I think the problem is this:
for i,v in ipairs(player.shots) do
if projectile.animNumber == 1 then
v.x = v.x + 300 * dt
elseif projectile.animNumber == 2 then
v.x = v.x - 300 * dt
elseif projectile.animNumber == 3 then
v.y = v.y + 300 * dt
else
v.y = v.y - 300 * dt
end
end
In your for loop you are checking for projectile.animNumber which doesn't appear previously in your code. Therefore, making the else statement true and causing all projectiles to travel in 1 direction.
Sorry if it was confusing; I'm not that good at explaining things

Index Exceeds Matrix Dimensions - Canny Edge Detection

I am using the following lines of code for edge detection using canny edge detector :
I=imread('bradd.tif');
figure,imshow(I);
IDtemp = im2double(I);
[r c]=size(I);
ID(r,c) = 0;
IDx(r,c) = 0;
IDfil(r,c) = 0;
IDxx(r,c) = 0;
IDy(r,c) = 0;
IDyy(r,c) = 0;
mod(r,c) = 0;
for i= 1 : r+4
for j = 1:c+4
if(i<=2 || j<=2 || i>=r+3 || j>=c+3)
ID(i,j) = 0;
else
ID(i,j) = IDtemp(i-2,j-2);
end;
end
end
%figure,imshow(ID);
filter=[2 4 5 4 2;4 9 12 9 4;5 12 15 12 5;4 9 12 9 4;2 4 5 4 2];
for i=1:5
for j=1:5
filter(i,j)=filter(i,j)/159;
end
end
%figure,imshow(filter);
for v = 3 : r
for u = 3 : c
sum = 0;
for i = -2 : 2
for j = -2 : 2
sum = sum + (ID(u+i, v+j) * filter(i+3, j+3));
end
end
IDx(u,v) = sum;
end
end
%figure,imshow(IDx);
IDxtemp = IDx;
for i= 1 : r+2
for j = 1:c+2
if(i<=1 || j<=1 || i>=r || j>=c)
IDfil(i,j) = 0;
else
IDfil(i,j) = IDxtemp(i-1,j-1);
end;
end
end
%figure,imshow(IDfil);
Mx = [-1 0 1; -2 0 2; -1 0 1]; % Sobel Mask in X-Direction
My = [-1 -2 -1; 0 0 0; 1 2 1]; % Sobel Mask in Y-Direction
for u = 2:r
for v = 2:c
sum1 = 0;
for i=-1:1
for j=-1:1
sum1 = sum1 + IDfil(u + i, v + j)* Mx(i + 2,j + 2);
end
end
IDxx(u,v) = sum1;
end;
end
%figure,imshow(IDxx);
for u = 2:r
for v = 2:c
sum2 = 0;
for i=-1:1
for j=-1:1
sum2 = sum2 + IDfil(u + i, v + j)* My(i + 2,j + 2);
end
end
IDyy(u,v) = sum2;
end
end
%figure,imshow(IDyy);
for u = 1:r
for v = 1:c
mod(u,v) = sqrt(IDxx(u,v)^2 + IDyy(u,v)^2) ;
%mod(u,v) = sqrt(IDxx(u,v)^2 + IDyy(u,v)^2);
end
end
%figure,imshow(mod);
modtemp = mod;
for i= 1 : r+2
for j = 1:c+2
if(i<=1 || j<=1 || i>=r || j>=c)
mod(i,j) = 0;
else
mod(i,j) = modtemp(i-1,j-1);
end;
end
end
%figure,imshow(mod);
theta(u,v) = 0;
supimg(u,v) = 0;
ntheta(u,v) = 0;
for u = 2 : r
for v = 2 : c
theta(u,v) = atand(IDyy(u,v)/IDxx(u,v));
if ((theta(u,v) > 0 ) && (theta(u,v) < 22.5) || (theta(u,v) > 157.5) && (theta(u,v) < -157.5))
ntheta(u,v) = 0;
end
if ((theta(u,v) > 22.5) && (theta(u,v) < 67.5) || (theta(u,v) < -112.5) && (theta(u,v) > -157.5))
ntheta(u,v) = 45;
end
if ((theta(u,v) > 67.5 && theta(u,v) < 112.5) || (theta(u,v) < -67.5 && theta(u,v) > 112.5))
ntheta(u,v) = 90;
end
if ((theta(u,v) > 112.5 && theta(u,v) <= 157.5) || (theta(u,v) < -22.5 && theta(u,v) > -67.5))
ntheta(u,v) = 135;
end
if (ntheta(u,v) == 0)
if (mod(u, v) > mod(u, v-1) && mod(u, v) > mod(u, v+1))
supimg(u,v) = mod(u,v);
else supimg(u,v) = 0;
end
end
if (ntheta(u,v) == 45)
if (mod(u, v) > mod(u+1, v-1) && mod(u, v) > mod(u-1, v+1))
supimg(u,v) = mod(u,v);
else supimg(u,v) = 0;
end
end
if (ntheta(u,v) == 90)
if (mod(u, v) > mod(u-1, v) && mod(u, v) > mod(u+1, v))
supimg(u,v) = mod(u,v);
else supimg(u,v) = 0;
end
end
if (ntheta(u,v) == 135)
if (mod(u, v) > mod(u-1, v-1) && mod(u, v) > mod(u+1, v+1))
supimg(u,v) = mod(u,v);
else supimg(u,v) = 0;
end
end
end
end
%figure,imshow(ntheta);
th = 0.2;
tl = 0.1;
resimg(u,v)= 0;
for u = 2 : r-1
for v = 2 : c-1
if(supimg(u,v) > th)
resimg(u,v) = 1;
else
if(supimg(u,v) >= tl && supimg(u,v) <= th )
resimg(u,v) = 1;
else
if (supimg(u,v) < tl)
resimg(u,v) = 0;
end
end
end
if (supimg(u-1,v-1) > th || supimg(u,v-1) > th || supimg(u+1,v-1) > th || supimg(u+1,v) > th || supimg(u+1,v+1) > th || supimg(u,v+1) > th || supimg(u-1,v+1) > th || supimg(u-1,v) > th)
resimg(u,v) = 1;
else
resimg(u,v) = 0;
end
end
end
figure,imshow(supimg);
figure,imshow(resimg);
However, for some of the images it is working fine, while for others it is showing the following error :
Index exceeds matrix dimensions.
Error in canny_edge (line 45)
sum = sum + (ID(u+i, v+j) * filter(i+3, j+3));
Can someone help me sort out this problem ??
Thanks and Regards.
Your loop ranges are in the wrong order leading to the error. If you modify your loop ranges to this
for u = 3 : r
for v = 3 : c
sum = 0;
for i = -2 : 2
for j = -2 : 2
sum = sum + (ID(u+i, v+j) * filter(i+3, j+3));
end
end
IDx(u,v) = sum;
end
end
the problem is solved.
My guess is that the code worked only for square images with c==r.
Note you are not making use of Matlab's vectorization capability, which allows you to shorten the first steps to:
ID = [zeros(2,c+4) ; [zeros(r,2) IDtemp zeros(r,2)]; zeros(2,c+4)];
filter=[2 4 5 4 2;4 9 12 9 4;5 12 15 12 5;4 9 12 9 4;2 4 5 4 2];
filter=filter/159;
for u = 1 : r
for v = 1 : c
IDx(u,v) = sum(reshape(ID(u+[0:4], v+[0:4]).* filter,25,1));
end
end
and this last loop can also be collapsed further but that might make readability an issue.
(edit) The loop can (for instance) be replaced with
IDx = conv2(ID, filter,'same');

Resources