sorting two dimensional array asp classic - asp-classic

So I have a 2d array that I want to Sort. I can sort it easily when one dimensional.
I Hope you can help me guys.
This is my Data.
top5(0,0) = Greeting
top5(0,1) = 2
top5(1,0) = VerifyingInformation
top5(1,1) = 5
top5(2,0) = Calibration
top5(2,1) = 4
I can sort It no problem when one dimensional.
I'm using this code for one dimensional.
For i = LBound(top5) to UBound(top5)
For j = LBound(top5) to UBound(top5) - 1
If top5(j,1) < top5(j + 1,1) Then
TempValue = top5(j + 1,1)
top5(j + 1,1) = top5(j,1)
top5(j,1) = TempValue
End If
next
Next
The result I want to have is this.
VerifyingInformation 5
Calibration 4
Greeting 2

THIS WORKS FOR ME
function sort_arr_mult(byref ArrTmp, ordPlace)
' ordPlace - the place of the order value in the array
' create the new array
Redim arrRet (Ubound(ArrTmp, 1), Ubound(ArrTmp, 2))
for j = 0 to Ubound(ArrTmp, 2)
orderVal = ArrTmp(ordPlace, j)
if j = 0 then ' first enter insert to first column
for i = 0 to Ubound(ArrTmp, 1)
arrRet(i, j) = ArrTmp(i, j)
next
else
' check the first value if smaller or equal
' move the columnbs one field up
' at the end insert to currenct column
for k = 0 to Ubound(arrRet, 2)
if isEmp(arrRet(0, k)) then ' if empty fied the column
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
else
if orderVal<=arrRet(ordPlace, k) then
for x = Ubound(arrRet, 2) to k+1 step -1
for i = 0 to Ubound(arrRet, 1)
arrRet(i, x) = arrRet(i, x-1)
next
next
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
end if
end if
next ' for k = 0 to Ubound(arrRet, 2)
end if
next
sort_arr_mult = arrRet
end function

It looks like you are actually performing a one-dimensional sort of the numeric value with an associated text string just along for the ride.
Your example code is close but you will need 2 temp values to represent the array values you will be shifting around.
For i = LBound(top5) to UBound(top5)
For j = LBound(top5) to UBound(top5) - 1
If top5(j,1) < top5(j + 1,1) Then
TempValue = top5(j + 1,1)
TempText = top5(j + 1,0)
top5(j + 1,1) = top5(j,1)
top5(j + 1,0) = top5(j,0)
top5(j,1) = TempValue
top5(j,0) = TempText
End If
Next
Next

Extending raam's answer with 3rd parameter as sorting direction "ASC" or "DESC"
Function sortArrayMulti(byref ArrTmp, ordPlace, so)
''so: sortorder "ASC" or "DESC"
Dim j, i, k, orderVal, x
Redim arrRet(Ubound(ArrTmp, 1), Ubound(ArrTmp, 2))
for j = 0 To Ubound(ArrTmp, 2)
orderVal = ArrTmp(ordPlace, j)
if j = 0 Then
for i = 0 to Ubound(ArrTmp, 1)
arrRet(i, j) = ArrTmp(i, j)
next
else
for k = 0 to Ubound(arrRet, 2)
if isEmpty(arrRet(0, k)) then
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
else
if so = "ASC" then
if orderVal <= arrRet(ordPlace, k) then
for x = Ubound(arrRet, 2) to k + 1 step -1
for i = 0 to Ubound(arrRet, 1)
arrRet(i, x) = arrRet(i, x - 1)
next
next
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
end if
else
if orderVal >= arrRet(ordPlace, k) then
for x = Ubound(arrRet, 2) to k + 1 step -1
for i = Ubound(arrRet, 1) to 0 step -1
arrRet(i, x) = arrRet(i, x - 1)
next
next
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
end if
end if
end if
next
end if
next
sortArrayMulti = arrRet
End Function

Related

Script Math's not correct lua minecraft

idk what is wrong but it writes this into the file when it saves the value. If u need more information or have a question please ask me! Thanks for your time and hopefully u your solution!
SebyGHG
My discord: S̸̽̚e̵̓̓b̸̿̕y̴͆͐#4638
Here is a screenshot of the time and the value in the file:
Value in file
Time in game
Code:
name = "Timer"
description = "Just a normal Timer."
positionX = 0
positionY = 0
sizeX = 24
sizeY = 10
scale = 1
START_STOP_KEY = 0x55 --or 'U'
RESET_KEY = 0x4A --or 'J'
--
--[[
Timer Module Script by SebyGHG original script by Onix64(Stopwatch)
if you wish to change the key you can take the key code from here
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
]] -------------script-code-------------
state = 1
stopTime = 0
startTime = 0
f = io.input("timesave.txt")
result = f :read()
f :close()
stopTime = result
state = 2
function keyboard(key, isDown)
if (isDown == true) then
if (key == RESET_KEY) then
state = 0
elseif (key == START_STOP_KEY) then
if (state == 0) then
state = 1
startTime = os.time()
elseif (state == 1) then
state = 2
io.output("timesave.txt")
timesave= (io.open("timesave.txt","w"))
io.write(stopTime)
io.close(timesave)
stopTime = os.time()
elseif (state == 2) then
state = 1
startTime =startTime + os.time() - stopTime
end
end
end
end
TimerText = "00:00"
TextColor = {r = 30, g = 255, b = 30, a = 255}
function doubleDigit(number)
if (number < 10) then
return "0" .. math.floor(number)
else
return math.floor(number)
end
end
function timeText(time)
local result = ""
local days = 0
while (time > 86399) do
days = days + 1
time = time - 86400
end
local hours = 0
while (time > 3599) do
hours = hours + 1
time = time - 86400
end
local minutes = 0
while (time > 59) do
minutes = minutes + 1
time = time - 60
end
if (days == 0) then
if (hours == 0) then
return doubleDigit(minutes) .. ":" .. doubleDigit(time)
else
return math.floor(hours) .. " : " .. doubleDigit(minutes) .. ":" .. doubleDigit(time)
end
else
return math.floor(days) ..
" : " .. doubleDigit(hours) .. " : " .. doubleDigit(minutes) .. ":" .. doubleDigit(time)
end
end
function update()
if (state == 0) then
TextColor = {r = 255, g = 0, b = 0, a = 255}
TimerText = "00:00"
elseif (state == 1) then
TimerText = timeText(os.time() - startTime)
TextColor = {r = 0, g = 255, b = 255, a = 255}
elseif (state == 2) then
TimerText = timeText(stopTime - startTime)
TextColor = {r = 255, g = 255, b = 0, a = 255}
end
end
function render()
local font = gui.font()
local tw = font.width(TimerText)
gfx.color(0, 0, 0, 0)
gfx.rect(0, 0, tw + 4, 10)
gfx.color(TextColor.r, TextColor.g, TextColor.b, TextColor.a)
gfx.text(2, 1, TimerText)
end
One error I spot is this part
while (time > 3599) do
hours = hours + 1
time = time - 86400
end
this is most likely supposed to be
while (time > 3599) do
hours = hours + 1
time = time - 3600
end

Dynamic (auto increment) input length (count) on a linear regression channel drawing, starting from given bar_index/datetime in Pine Script language

My question is about the Linear Regression drawing.
The example in the documentation uses a fixed length (100), and is therefore :
shifting to the right on each new bar
of constant width (here 100 bars)
I'm trying to make it start from a custom point in time (x bars from now or bar_index or datetime...), so that :
it keeps extending on each new bar
but the starting point remains on the same location (until we change it in the settings).
That means that the length (input) would be dynamic and increase on each new bar.
I am getting the following error : Pine cannot determine the referencing length of a series. Try using max_bars_back in the study or strategy function.
Is it possible to do ?
Here is the code
//#version=4
study("Linear Regression", shorttitle="LinReg", overlay=true)
upperMult = input(title="Upper Deviation", defval=2)
lowerMult = input(title="Lower Deviation", defval=-2)
useUpperDev = input(title="Use Upper Deviation", defval=true)
useLowerDev = input(title="Use Lower Deviation", defval=true)
showPearson = input(title="Show Pearson's R", defval=true)
extendLines = input(title="Extend Lines", defval=false)
// ====================================================================
// ====================================================================
// Original parameter (the one that should increments)
// len = input(title="Count", defval=100)
// Unsuccessful attempt : "Starting from given bar_index"
barIndexOfStartingBar = 6392 - 80 // 6392 : Current bar_index, 80 : Offset to the starting bar
len = bar_index - barIndexOfStartingBar
len := nz(len[1]) + 1
// Unsuccessful attempt : "x bars from current bar"
startingPointFromCurrentBar = input(title="Count", defval=80)
len = (bar_index + startingPointFromCurrentBar) - bar_index
len := nz(len[1]) + 1
// ====================================================================
// ====================================================================
src = input(title="Source", defval=close)
extend = extendLines ? extend.right : extend.none
calcSlope(src, len) =>
if not barstate.islast or len <= 1
[float(na), float(na), float(na)]
else
sumX = 0.0
sumY = 0.0
sumXSqr = 0.0
sumXY = 0.0
for i = 0 to len - 1
val = src[i]
per = i + 1.0
sumX := sumX + per
sumY := sumY + val
sumXSqr := sumXSqr + per * per
sumXY := sumXY + val * per
slope = (len * sumXY - sumX * sumY) / (len * sumXSqr - sumX * sumX)
average = sumY / len
intercept = average - slope * sumX / len + slope
[slope, average, intercept]
[s, a, i] = calcSlope(src, len)
startPrice = i + s * (len - 1)
endPrice = i
var line baseLine = na
if na(baseLine) and not na(startPrice)
baseLine := line.new(bar_index - len + 1, startPrice, bar_index, endPrice, width=1, extend=extend, color=color.red)
else
line.set_xy1(baseLine, bar_index - len + 1, startPrice)
line.set_xy2(baseLine, bar_index, endPrice)
na
calcDev(src, len, slope, average, intercept) =>
upDev = 0.0
dnDev = 0.0
stdDevAcc = 0.0
dsxx = 0.0
dsyy = 0.0
dsxy = 0.0
periods = len - 1
daY = intercept + (slope * periods) / 2
val = intercept
for i = 0 to periods
price = high[i] - val
if (price > upDev)
upDev := price
price := val - low[i]
if (price > dnDev)
dnDev := price
price := src[i]
dxt = price - average
dyt = val - daY
price := price - val
stdDevAcc := stdDevAcc + price * price
dsxx := dsxx + dxt * dxt
dsyy := dsyy + dyt * dyt
dsxy := dsxy + dxt * dyt
val := val + slope
stdDev = sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / sqrt(dsxx * dsyy)
[stdDev, pearsonR, upDev, dnDev]
[stdDev, pearsonR, upDev, dnDev] = calcDev(src, len, s, a, i)
upperStartPrice = startPrice + (useUpperDev ? upperMult * stdDev : upDev)
upperEndPrice = endPrice + (useUpperDev ? upperMult * stdDev : upDev)
var line upper = na
lowerStartPrice = startPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
lowerEndPrice = endPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
var line lower = na
if na(upper) and not na(upperStartPrice)
upper := line.new(bar_index - len + 1, upperStartPrice, bar_index, upperEndPrice, width=1, extend=extend, color=#0000ff)
else
line.set_xy1(upper, bar_index - len + 1, upperStartPrice)
line.set_xy2(upper, bar_index, upperEndPrice)
na
if na(lower) and not na(lowerStartPrice)
lower := line.new(bar_index - len + 1, lowerStartPrice, bar_index, lowerEndPrice, width=1, extend=extend, color=#0000ff)
else
line.set_xy1(lower, bar_index - len + 1, lowerStartPrice)
line.set_xy2(lower, bar_index, lowerEndPrice)
na
// Pearson's R
var label r = na
transparent = color.new(color.white, 100)
label.delete(r[1])
if showPearson and not na(pearsonR)
r := label.new(bar_index - len + 1, lowerStartPrice, tostring(pearsonR, "#.################"), color=transparent, textcolor=#0000ff, size=size.normal, style=label.style_labelup)
With these changes you should be able to go back a few thousand bars:
study("Linear Regression", shorttitle="LinReg", overlay=true, max_bars_back = 4999)
and either one of these:
// #1
offsetToStart = input(100, minval = 1, step = 100)
len = max(1, offsetToStart)
// #2
startingBar = input(10000, minval = 1, step = 100)
len = max(1, bar_index - startingBar)
What you are looking for is called anchoring. You can do this with a trick. What I usually do is the following:
p = input("D", title="Period", type=input.resolution)
offsetToStart = input(0, title="Begin Offset") // remove the minval
newbar(p) => change(time(p)) == 0?0:1
nb = newbar(p)
bars = barssince(nb) + offsetToStart
var line l1 = na
l1 := line.new(bar_index[bars], open, bar_index[bars], open + syminfo.mintick, extend=extend.both)
line.delete(l1[1])
Remember to remove the minval. Chose the period and you can now anchor beginning of your indicator. Playaround with p

Calculating a determinant in Lua

I'm trying to calculate determinants with any order using Lua. I can calculate determinants for order less than 4, but not for greater equals than 4 ones.
I have a 4x4 matrix and its determinant with the program is 0, but the real solution is 56.
I don't know if the problem is in getSubmatrix method or is in detMat method, because I don't have any error message from the console.
I've ported the methods from my own java code, where it works fine.
Here's all my code:
function numMat(n, A)
local S = {}
for i = 1, #A, 1 do
local T = {}
S[i] = T
for j =1, #A[1], 1 do
T[j] = n * A[i][j]
end
end
return S
end
function sumMat(A, B)
local C = {}
for i = 1, #A do
local D = {}
C[i] = D
for j = 1, #A[1] do
D[j] = A[i][j] + B[i][j]
end
end
return C
end
function subMat(A, B)
return sumMat(A, numMat(-1, B))
end
function printMatrix(A)
for i, v in ipairs(A) do
for j, w in ipairs(v) do
print(w)
end
end
end
function escalarProduct(u, v)
local w = 0
for i = 1, #u do
w = w + u[i] * v[i]
end
return w
end
function prodMat(A, B)
local C = {}
for i = 1, #A do
C[i] = {}
for j = 1, #B[1] do
local num = A[i][1] * B[1][j]
for k = 2, #A[1] do
num = num + A[i][k] * B[k][j]
end
C[i][j] = num
end
end
return C
end
function powMat(A, power)
local B = {}
local C = {}
C = A
for i = 1, power - 1 do
B = prodMat(C, A)
C = B
end
return B
end
function trasposeMat(A)
local B = {}
for i = 1, #A do
local C = {}
B[i] = C
for j = 1, #A[1] do
C[j] = A[j][i]
end
end
return B
end
function productDiag(m)
local prod = 1
for i = 1, #m do
for j = 1, #m do
if i == j then prod = prod * m[i][i] end
end
end
return prod
end
function isDiagonal(A)
for i = 1, #A do
for j = 1, #A do
if i ~= j and A[i][j] ~= 0 then return false end
end
end
return true
end
function isTriangSup(m)
for i = 1, #m do
for j = 1, i do
if m[i][j] == 0 then return true end
end
end
return false
end
function isTriangInf(m)
return isTriangSup(trasposeMat(m))
end
function isTriang(m)
if(isTriangSup(m)) then return true
else
return false
end
end
function getSubmatrix(A, rows, cols, col)
local submatrix = {}
local k = 1
for j = 1, cols do
--local D = {}
--submatrix[j] = D
if j == col then
break
end
for i = 2, rows do
submatrix[i-1][k] = A[i][j]
--D[k] = A[i][j]
end
k = k + 1
end
return submatrix
end
function det2Mat(A)
assert(#A == 2 and #A == #A[1], 'Error: The matrix must be squared, order 2.')
return A[1][1] * A[2][2] - A[1][2] * A[2][1]
end
function det3Mat(A)
assert(#A == 3 and #A == #A[1], 'Error: The matrix must be squared, order 3.')
s1 = A[1][1] * A[2][2] * A[3][3] + A[2][1] * A[3][2] * A[1][3] + A[1][2] * A[2][3] * A[3][1]
s2 = A[1][3] * A[2][2] * A[3][1] + A[1][2] * A[2][1] * A[3][3] + A[2][3] * A[3][2] * A[1][1]
return s1 - s2
end
function detMat(A)
local submatrix = {}
local det
local sign = 1
local rows = #A
local cols = #A[1]
assert(rows == cols, 'Error: The matrix must be squared.')
if rows == 1 then
return A[1][1]
end
if rows == 2 then
return det2Mat(A)
end
if rows == 3 then
return det3Mat(A)
end
if isDiagonal(A) or isTriang(A) then return productDiag(A) end
if rows > 3 then
for column = 1, cols do
submatrix = getSubmatrix(A, rows, cols, column)
det = det + sign * A[1][column] * detMat(submatrix)
sign = -sign
end
end
return det
end
A = {{1, 3}, {5, 6}}
B = {{2, 4}, {3, 1}}
C = {{2, 3, 4}, {-5, 4, 7}, {7, 1, 0}}
D = {{2, 0, 0, 0}, {0, 4, 0, 0}, {0, 0, 7, 0}, {0, 0, 0, 6}}
E = {{2, 3, 4, -3}, {-5, 4, 7, -2}, {7, 1, 0, 5}, {3, 4, 5, 6}}
--printMatrix(numMat(-1, A))
--printMatrix(sumMat(A, B))
--printMatrix(subMat(A, B))
--print(escalarProduct({1, 3}, {5, 6}))
--printMatrix(prodMat(A, B))
--printMatrix(trasposeMat(A))
--printMatrix(powMat(A, 2))
--printMatrix(powMat(A, 3))
print(detMat(A))
print(detMat(B))
print(detMat(C))
print(detMat(D))
print(detMat(E)) --The solution must be 56
And the console solution is:
-9
-10
1
336
0
The error is when I want to find out the determinant of the matrix E.

run-time error '5' maps.googleapis.com/maps/api/distancematrix

Yesterday, I ran the following script in excel and it worked, but now it throws an error:
(run-time error '5' )
in the line:
Distance = Mid(Distance, InStr(1, Distance, ">") + 1, InStr(1, Distance, " ") - InStr(1, Distance, ">") - 1)
Sub Поиск_ближайшего_СБС()
Set Points = Worksheets("Точки")
Set SBS = Worksheets("СБС")
Dim Cordinate11, Cordinate12, Cordinate21, Cordinate22 As String
i = 2
Do While SBS.Cells(i, 1) <> Empty
Rows_of_SBS = i
i = i + 1
Loop
i = 2
Do While Cells(i, 1) <> Empty
Dist = 1000
Cordinate21 = Points.Cells(i + 1, 3)
Cordinate22 = Points.Cells(i + 1, 4)
For j = 2 To Rows_of_SBS
Cordinate11 = SBS.Cells(j, 2)
Cordinate12 = SBS.Cells(j, 3)
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
With IE
.Navigate "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" _
& Cordinate11 & ",+" & Cordinate12 _
& "&destinations=" _
& Cordinate21 & ",+" & Cordinate22 _
& "&mode=driving&language=ru&sensor=false"
Do While .Busy
DoEvents
Loop
For Each MyTable In .Document.getElementsByTagName("text")
Distance = MyTable.innertext
Next
Distance = Mid(Distance, InStr(1, Distance, ">") + 1, InStr(1, Distance, " ") - InStr(1, Distance, ">") - 1)
If CDbl(Distance) < Dist Then
Dist = CDbl(Distance) s = j
End If
End With
IE.Quit
Set EI = Nothing
Next j
Points.Cells(i + 1, 5) = Dist
Points.Cells(i + 1, 6) = SBS.Cells(s, 1)
i = i + 1
Loop
End Sub
'

Sorting multidimensional array by date

I know this topic was opened so many times before.
I looked at all the examples, but I just cant make it work!
I have 3 dimensions, date is in the first dimension.
All works fine expect one array, and I just dont understand why..
Here is the test link: click here
Sub DataSorter(arrArray)
Dim row, j, StartingKeyValue, StartingOtherValue, _
NewStartingKey, NewStartingOther, _
swap_pos
For row = 0 To UBound(arrArray)-1
StartingKeyValue = arrArray(row, 0)
StartingOtherValue = arrArray(row, 0)
StartingVenue = arrArray(row, 1)
StartingVenueOther = arrArray(row, 1)
swap_pos = row
For j = row + 1 to UBound(arrArray)
If DateDiff("s", arrArray(j, 0), NewStartingKey) < 0 Then
swap_pos = j
NewStartingKey = arrArray(j, 0)
NewStartingOther = arrArray(j, 0)
NewStartingVenue = arrArray(j, 1)
NewStartingVenueOther = arrArray(j, 1)
NewStartingCountry = arrArray(j, 2)
NewStartingCountryOther = arrArray(j, 2)
End If
Next
If swap_pos <> row Then
arrArray (swap_pos, 0) = StartingKeyValue
arrArray (swap_pos, 0) = StartingOtherValue
arrArray (row, 0) = NewStartingKey
arrArray (row, 0) = NewStartingOther
arrArray (swap_pos, 1) = StartingVenue
arrArray (swap_pos, 1) = StartingVenueOther
arrArray (row, 1) = NewStartingVenue
arrArray (row, 1) = NewStartingVenueOther
arrArray (swap_pos, 2) = StartingCountry
arrArray (swap_pos, 2) = StartingCountryOther
arrArray (row, 2) = NewStartingCountry
arrArray (row, 2) = NewStartingCountryOther
End If
Next
End Sub
Very strange code. Try this:
Sub DataSorter(arrArray)
Dim row, i, tmp
For row = 0 To UBound(arrArray) - 1
For j = row + 1 to UBound(arrArray)
If DateDiff("s", arrArray(j, 0), arrArray(row, 0)) < 0 Then
For i = 0 To 2
tmp = arrArray(j, i)
arrArray(j, i) = arrArray(row, i)
arrArray(row, i) = tmp
Next
End If
Next
Next
End Sub

Resources