sqlRows = rst.GetRows()
i = 0
For Each element In sqlRows
If i > 0 And i < sizeOfState + 1 Then
SmartTags("visu_state_on")(i - 1) = element
ElseIf i > sizeOfState And i < 2 * sizeOfState + 1 Then
SmartTags("visu_state_off")(i - sizeOfState - 1) = element
ElseIf i > (2 * sizeOfState ) And i < 2 * sizeOfState + sizeOfMeasurement + 1 Then
SmartTags("visu_limits_right")(i - (2 * sizeOfState - 1)) = element
ElseIf i > 2 * sizeOfState + sizeOfMeasurement And i < 2 * (sizeOfStanja + sizeOfMeasurement ) + 1 Then
SmartTags("visu_limits_left")(i - (2 * sizeOfState + sizeOfMeasurement )) = element
End If
i = i + 1
Next
With code above I'm looping through array sqlRows and with variable i I'm filling other four arrays with data from sqlRows.
This solution works but I'm wondering is there more elegant way to achieve the same.
sqlRows is array with dimensions 343x1,
visu_state_on is array with dimensions 8x1,
visu_state_off is array with dimensions 8x1,
visu_limits_right is array with dimensions 160x1,
visu_limits_left is array with dimensions 160x1,
and variables sizeOfState and sizeOfMeasurement are just there that I can calculate indexes for these four arrays.
For one thing you can remove the first clause from each of your conditions, because they will always be true
i starts with the value 0 and is always incremented, so the value will never be less than zero.
If a value is not less than n + 1 (condition in previous ElseIf) then it's guaranteed to be greater than n.
Also, VBScript does have a <= comparison operator, so it's better to compare i <= n rather than i < n + 1.
I would also recommend calculating values that won't change inside the loop just once outside the loop. Re-calculating them with each loop cycle is a waste of resources.
Adding an Else branch to handle values greater than 2 * (sizeOfStanja + sizeOfMeasurement) might be a good idea too.
sqlRows = rst.GetRows()
i = 0
ref1 = 2 * sizeOfState
ref2 = ref1 + sizeOfMeasurement
ref3 = 2 * (sizeOfStanja + sizeOfMeasurement)
For Each element In sqlRows
If i <= sizeOfState Then
SmartTags("visu_state_on")(i - 1) = element
ElseIf i <= ref1 Then
SmartTags("visu_state_off")(i - sizeOfState - 1) = element
ElseIf i <= ref2 Then
SmartTags("visu_limits_right")(i - ref1 + 1) = element
ElseIf i <= ref3 Then
SmartTags("visu_limits_left")(i - ref2) = element
Else
'handle i > ref3 here
End If
i = i + 1
Next
Related
I am trying to compare the 2 arrays for common values, delete the common values and make a single array by concatenating 2 arrays.
Facing issue (out of bounds, array size is 3) while running the loop with array index size reference
//#version=4
study("`array.concat()`")
a = array.new_float(0)
b = array.new_float(0)
array.push(a, 0)
array.push(a, 1)
array.push(a, 2)
array.push(a, 4)
array.push(b, 0)
array.push(b, 1)
array.push(b, 3)
array.push(b, 4)
disjoint(e,f)=>
c = e
d = f
for i=0 to array.size(e)-1
for j=0 to array.size(f)-1
if array.get(e,i) == array.get(f,j)
array.remove(c,i)
array.remove(d,i)
array.concat(c , d)
z = disjoint(a,b)
if barstate.islast
l1 = label.new(bar_index, 0, "AFTER\na: " + tostring(a) + "\nb: " + tostring(b) + "\nZ: " + tostring(z), style = label.style_label_up, size = size.normal)
label.delete(l1[1])
I am trying to find the max value inside a parameterized array, I was looking at this post and came across forloop-generate. The only change I want to make is change that approach of a explicit array to parameterized array, but I am running into genvar assignment problem.
reg [$clog2(LENGTH)-1:0] arr [0:LENGTH-1];
wire [$clog2(LENGTH)-1:0] value_l1[0:LENGTH-2];
wire [$clog2(LENGTH)-1:0] index_l1[0:LENGTH-2];
genvar gen_i, gen_j, gen_k;
generate
for(gen_i = 0; gen_i < LENGTH; gen_i = gen_i + 2) begin : loop1
Compare cmp1(.A(arr[i])
,.B(arr[i+1])
,.indexA(i)
,.indexB(i+1)
,.value(value_l1[i/2])
,.index(index_l1[i/2])
);
end
gen_k = 0;
for(gen_i = 1; gen_i < $clog2(LENGTH); gen_i = gen_i + 1) begin : loop2_1
for(gen_j = 0; gen_j < LENGTH / (2*gen_i); gen_j = gen_j + 2) begin : loop2_2
Compare cmp2(.A(value_l1[gen_k + gen_j])
,.B(value_l1[gen_k + gen_j+1])
,.indexA(index_l1[gen_k + gen_j])
,.indexB(index_l1[gen_k + gen_j+1])
,.value(value_l1[(LENGTH/(2*gen_i))+(gen_j/2)])
,.index(index_l1[(LENGTH/(2*gen_i))+(gen_j/2)])
);
end
//gen_k = gen_j; // <--------- problem here
end
endgenerate
My approach is to have a list of the compared elements, and then compare that list and write back the result to the same list. For example:
original array : 0 1 2 3 4 5 6 7
would results into
compare array : 1 3 5 7 | 3 7 | 7
^ ^
| |
| largest element
|
gen_k + gen_j
then output 7 as the largest element, but I cannot do gen_k = gen_j to save the index (offset to the second part of the compare array). Is it even possible to use generate with parameterized array? If so, how can I fix this problem?
You can do this with an intermediate parameter.
for(gen_i = 1; gen_i < $clog2(LENGTH); gen_i = gen_i + 1) begin : loop2_1
parameter param_k = LENGTH / (2*gen_i) + 1;
for(gen_j = 0; gen_j < LENGTH / (2*gen_i); gen_j = gen_j + 2) begin : loop2_2
Compare cmp2(.A(value_l1[param_k + gen_j])
,.B(value_l1[param_k + gen_j+1])
,.indexA(index_l1[param_k + gen_j])
,.indexB(index_l1[param_k + gen_j+1])
,.value(value_l1[(LENGTH/(2*gen_i))+(gen_j/2)])
,.index(value_l1[(LENGTH/(2*gen_i))+(gen_j/2)])
);
end
end
I am displaying my results from an SQL search in excel from a WPF application. It initally displays them in a listview and the user can select the option to export to excel which is working fine.
The error i am having is, if there are say 5 results in the SQL result and displayed in the listview, when i export to excel it is only displaying 4 results plus the SQL table headings...
For i As Integer = 1 To dtMainSQLData.Rows.Count
For j As Integer = 1 To dtMainSQLData.Columns.Count
If i = 1 Then
sheet.Cells(i, j) = dcCollection(j - 1).ToString()
Else
sheet.Cells(i, j) = dtMainSQLData.Rows(i - 1)(j - 1).ToString()
End If
Next
Next
Does anyone see a problem with the about loop for printing out the SQL results to excel
this is your problem:
For i As Integer = 1 To dtMainSQLData.Rows.Count
If you skip the first and you handle decreasing it by 1 when you access your items, you must go up to To dtMainSQLData.Rows.Count + 1
Personally I would start from 0 like this:
For i As Integer = 0 To dtMainSQLData.Rows.Count
For j As Integer = 0 To dtMainSQLData.Columns.Count
If i = 0 Then
sheet.Cells(i + 1, j + 1) = dcCollection(j).ToString()
Else
sheet.Cells(i + 1, j + 1) = dtMainSQLData.Rows(i)(j).ToString()
End If
Next
Next
After debugging csharpwinphonexaml's answer I got it working. I have to add an additional IF to check if it was at the last position.
For i As Integer = 0 To dtMainSQLData.Rows.Count
For j As Integer = 0 To dtMainSQLData.Columns.Count - 1
If i = 0 Then
sheet.Cells(i + 1, j + 1) = dtMainSQLData.Columns(j).ToString()
ElseIf i = dtMainSQLData.Rows.Count Then
sheet.Cells(i + 1, j + 1) = dtMainSQLData.Rows(0)(j).ToString()
Else
sheet.Cells(i + 1, j + 1) = dtMainSQLData.Rows(i)(j).ToString()
End If
Next
Next
I wrote a 1D array sorting code, however the first value disappears after sorting. Here is my code:
For i = 0 To 10 - 1
For j = 0 To (10 - 1) - i
If Xs(j) > Xs(j + 1) Then
tmp = Xs(j)
Xs(j) = Xs(j + 1)
Xs(j + 1) = tmp
End If
Next j
Next i
Original array:
0.995136318967065
1.92659411953677E-02
0.075211466386023
0.276865639306513
0.796949177428061
0.644136557566409
0.912439108707731
0.318021611061513
0.863316048056547
0.469710111256482
Array after sorting:
0
1.92659411953677E-02
0.075211466386023
0.276865639306513
0.318021611061513
0.469710111256482
0.644136557566409
0.796949177428061
0.863316048056547
0.912439108707731
I suspect you have dimensioned your array 0 To 10:
Dim Xs(0 To 10)
so creating 11 spaces into it. However, you have only 10 of them so you will have an Empty cell that will put itself on the first position at some point of the loop (being it evaluated as 0, which is indeed the lowest value into your dataset).
I also believe you have dimensioned your array 0 To 10 instead of 0 To 9 because, if not, you would have got an Subscript out of range error in this part of the code:
For j = 0 To (10 - 1) - i
In order to make your code work:
a) Change the Dim Xs(0 To 10) to Dim Xs(0 To 9)
b) Change the For j = 0 To (10 - 1) - i to For j = 0 To (10 - 1) - i-1
I have a user defined Excell worksheet function (Linear) that interpolates from an array of X and an array of Y values at a defined X1 value, which works fine. I have tried to use this within another function (NPL in the example code below) be setting it a a Private Static function within a VBA module and then calling the function using arrays of data created within the function.
When I use this in the spreadsheet I get a #VALUE error.
Any ideas what I am doing wrong?
Example code:
Function NPL(Length, Beam)
A = Array(1, 2, 3, 4)
B = Array(2, 4, 6, 8)
C = Linear(A, B, 1.5)
NPL = C
End Function
Private Static Function Linear(X, Y, X1)
N = 0
I = 1
Do
N = I
I = I + 1
Loop Until X(I) < X(I - 1) Or N = X.Count
A = 0
I = 0
Do
I = I + 1
Loop Until X(I) > X1 Or I > N - 1
If X1 < X(N) And X1 > X(1) Then
Linear = Y(I - 1) + (X1 - X(I - 1)) * (Y(I) - Y(I - 1)) / (X(I) - X(I - 1))
ElseIf X1 > X(N) Or X1 = X(N) Then
Linear = Y(N)
Else
Linear = Y(1)
End If
End Function
Replace your
Do
N = I
I = I + 1
Loop Until X(I) < X(I - 1) Or N = X.Count
with
Do
N = I
I = I + 1
Loop Until X(I) < X(I - 1) Or N = UBound(X) - LBound(X) + 1
This should work for any 1D array.