Rank numbers in a loop vb.net - vb.net

I currently pull data from a database and rank them when i loop through them. Example of such numbers are 45, 45, 67, 99, 34, 65, 88, 22, 90, 90, 90, 23, 55, 46. These are a total of 14 numbers, I want to loop through and assign rank.
Dim i As Integer() = {45, 45, 67, 99, 34, 65, 88, 22, 90, 90, 90, 23, 55, 46}
Dim lastScore As Integer
Dim position As Integer = 0
For Each i1 In i
If Val(lastScore) <> Val(i1) Then
position += 1
Console.WriteLine(position & vbCrLf)
ElseIf Val(lastScore) = Val(i1) Then
Console.WriteLine(position & vbCrLf)
position += 1
End If
lastScore = Val(i1)
Next
The current output of the code above is:
1, 1, 3, 4, 5, 6, 7, 8, 9, 9, 10, 12, 13, 14
Which is wrong. The expected output is supposed to be:
1, 1, 3, 4, 5, 6, 7, 8, 9, 9, 9, 12, 13, 14
How can I achieve this?

Here is an ugly code which creates the expected output:
Dim i As Integer() = {45, 45, 67, 99, 34, 65, 88, 22, 90, 90, 90, 23, 55, 46}
Dim lastScore As Integer
Dim lastScorePosition As Integer
Dim position As Integer = 1
For Each i1 In i
If Val(lastScore) <> Val(i1) Then
Console.Write(position & ",")
lastScorePosition = position
lastScore = Val(i1)
Else
Console.Write(lastScorePosition & ",")
End If
position += 1
Next

The expected result is not correct. I.e. why there is not rank 2?
Simple ranking is achieved with relatively simple code:
Sub Main()
Dim i As Integer() = {45, 45, 67, 99, 34, 65, 88, 22, 90, 90, 90, 23, 55, 46}
Dim lastScore As Integer
Dim position As Integer
Dim sb As New StringBuilder
For Each i1 In i
If Not lastScore = i1 Then position += 1
sb.Append(position & ", ")
lastScore = i1
Next
sb.Remove(sb.Length - 2, 2)
Console.WriteLine(sb.ToString)
Console.ReadLine()
End Sub
The output is:
1, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10, 11

Related

Extract specific number from a textboxes

How can I extract from this Textbox, for example what is in parentheses (9,2,8)
Textbox1.Text =
1, 3, 5, 6, 7, 11, 12, 13, 14, 20 (9)
5, 6, 10, 11, 12, 15, 17, 18, 19, 20 (2)
2, 3, 5, 6, 11, 13, 17, 18, 19, 20 (8)
And display in another textbox, Textbox2.Text = 9,2,8
There is another way of doing it.
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim s1 = "1, 3, 5, 6, 7, 11, 12, 13, 14, 20 (9)
5, 6, 10, 11, 12, 15, 17, 18, 19, 20 (2)
2, 3, 5, 6, 11, 13, 17, 18, 19, 20 (8)"
Dim re = New Regex("\(([^)]*)\)")
Dim things = re.Matches(s1)
For Each m As Match In things
Console.WriteLine(m.Groups(1).Value)
Next
Console.ReadLine()
End Sub
End Module
Outputs:
9
2
8
For this regular expression, I think a railroad diagram helps to explain what it does:
From https://regexper.com/#%5C%28%28%5B%5E%29%5D*%29%5C%29
You can achieve your desired result using a simple While loop
<TestMethod()>
Public Sub ExtractNumbersInParentheses()
Dim inputString As String = "1, 3, 5, 6, 7, 11, 12, 13, 14, 20 (9)
5, 6, 10, 11, 12, 15, 17, 18, 19, 20 (2)
2, 3, 5, 6, 11, 13, 17, 18, 19, 20 (8)"
Dim finalResult As String = String.Empty
Dim startingPosition As Integer = inputString.IndexOf("(", 0)
While (startingPosition > 0)
If (finalResult.Length > 0) Then finalResult += ", "
Dim extractedText = inputString.Substring(startingPosition + 1, 1)
finalResult += extractedText
startingPosition = inputString.IndexOf("(", startingPosition + 1)
End While
Debug.Print(finalResult)
End Sub
The key part is the .IndexOf function and each time you query the inputString you move the starting position to be beyond the current position otherwhise you will see the same parenthesis
After I ran this test, the output was:
9, 2, 8

Split Multiline Textbox Step by Step

how should i do if i want to add step by step values, I tried to make a code but I don't know how to make it useful,
Code:
Dim line() As String = TxtMReadOnly1.Lines
For i As Integer = 0 To line.Length - 2 Step 2 'Add this
''''
Dim Mynumber As Integer
Dim isEven As Boolean
Dim line() As String = TxtBoxIntDraws1.Lines
For i As Integer = 0 To line.Length - 1 'Add this
Mynumber = line.Length - 1
If Mynumber Mod 2 = 0 Then
isEven = True
TxtOutpVal1.Text = Mynumber
Else
TxtOutpVal2.Text = Mynumber
End If
Next
Textbox:
7, 12, 14, 17, 19, 22, 24, 29, 32, 37, 40, 48, 49, 58, 62,
5, 11, 13, 15, 19, 22, 24, 25, 35, 37, 38, 43, 45, 47, 50,
2, 6, 8, 9, 18, 22, 23, 24, 35, 39, 45, 49, 52, 53, 58, 60,
8, 11, 13, 14, 15, 17, 20, 26, 31, 32, 44, 47, 57, 60, 62,
1, 7, 8, 13, 14, 16, 19, 30, 31, 33, 38, 48, 62, 64, 65, 67,
1, 7, 11, 15, 21, 22, 24, 39, 42, 46, 50, 54, 59, 63, 66,
3, 5, 8, 9, 16, 17, 27, 30, 31, 34, 35, 37, 40, 44, 46, 50,
4, 6, 11, 17, 23, 33, 36, 39, 47, 51, 57, 59, 62, 63, 67,
Expected Output: Textbox1.Lines:
7, 12, 14, 17, 19, 22, 24, 29, 32, 37, 40, 48, 49, 58, 62,
2, 6, 8, 9, 18, 22, 23, 24, 35, 39, 45, 49, 52, 53, 58, 60,
1, 7, 8, 13, 14, 16, 19, 30, 31, 33, 38, 48, 62, 64, 65, 67,
3, 5, 8, 9, 16, 17, 27, 30, 31, 34, 35, 37, 40, 44, 46, 50,
Expected Output: Textbox2.Lines:
5, 11, 13, 15, 19, 22, 24, 25, 35, 37, 38, 43, 45, 47, 50,
8, 11, 13, 14, 15, 17, 20, 26, 31, 32, 44, 47, 57, 60, 62,
1, 7, 11, 15, 21, 22, 24, 39, 42, 46, 50, 54, 59, 63, 66,
4, 6, 11, 17, 23, 33, 36, 39, 47, 51, 57, 59, 62, 63, 67,
You can place each line of TextBox1 into an array. Then loop through that array and fill your additional textboxes based on the array index being odd or even.
'Create an array of each line in TextBox1
Dim arr = TextBox1.Lines
'Iterate through the array
For index = 0 To arr.Length - 1
'If the array index is divisible by 2 then add line to TextBox2
If index Mod 2 = 0 Then
TextBox2.AppendText(arr(index) & Environment.NewLine)
Else 'Add line to textbox3 if the index is NOT divisible by 2
TextBox3.AppendText(arr(index) & Environment.NewLine)
End If
Next

Cannot convert system.object[] to system.byte[], solutions and workarounds

Trying to create a function to simplify ratios (stored as arrays of length 2, with the index 0 being the numerator and index 1 being the denominator). This function gives me an error when the values passed in are object attributes.
Here is the function:
Function SimplifyRatio(Numerator, Denominator)
Dim i As Integer
Dim PNA() As Integer
i = 0
PNA = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199}
While i < PNA.Length()
If Numerator Mod PNA(i) = 0 And Denominator Mod PNA(i) = 0 Then
Numerator = Numerator / PNA(i)
Denominator = Denominator / PNA(i)
i = 0
End If
i = i + 1
End While
Return {Numerator, Denominator}
End Function
And here is the method containing the erroneous function call:
Public Function CalculateRatio()
Dim ratio(1) As Byte
ratio = SimplifyRatio(Gear.Teeth, Pinion.Teeth)
Return ratio
End Function
Any help would be appreciated,
Yours Sincerely,
KyuSiik

String to double array

I need to take this string:
Dim tmpTry As String = "10, 20, 30, 40, 50, 52, 20, 20, 10, 35, 3, 8, 47, 7, 2, 5, 55, 8, 0, 0, 6, 55, 0, 2, 12, 0, 0, 21, 14, 0, 3"
And convert it to a double array:
Dim arrNumOfVisits As Double() = New Double(tmpTry) {}
How do i go about doing that?
FYI the arrNumOfVisits goes into a ParamArray System.Collections.IEnumerable()
David
Dim arrString As String() = tmpTry.Split(New Char() {" "C})
Dim arrNumOfVisits As Double() = New Double(arrString.Length) {}
Dim i As Integer = 0
While i < arrString.Length
arrNumOfVisits(i) = Double.Parse(arrString(i))
i += 1
End While
The above code will do the trick, using regEx on this would be overkill.
Never the less do try to learn the basic RegEx operations, here are my favorite cheat sheets:
http://regexlib.com/CheatSheet.aspx?AspxAutoDetectCookieSupport=1
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

How to set an array to a list of values in VB.NET?

I cannot figure out how to set an array to one of two sets of numbers (there will be more later), every way that I have tried throws some kind of error. I have tried to Dim the array inside the case statements, but then I cannot use the array in the For Each, which makes this worthless.... any ideas would be appreciated.
Code:
Dim HourArray() As Integer
Select Case CurrentShapeRow(ROW_PERIOD)
Case "ON", "2X16"
HourArray = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}
Case "2X8", "5X8"
HourArray = {0, 1, 2, 3, 4, 5, 22, 23}
Case Else
Throw New Exception(String.Format("Unhandled Period: {0}", CurrentShapeRow(ROW_PERIOD)))
End Select
For Each HourCount As Integer In HourArray()
'DO SOME STUFF HERE
Next
HourArray = New Integer() {1,2,3,4,5,6,7,8,9}
When you assign an array to an existing variable you must use a constructor explicitly:
HourArray = New Integer() { 6, 7, 8, 9, 10, 11, 12, 13 }
This differs from a declaration and assignment where the constructor is optional:
Dim HourArray() As Integer = { 6, 7, 8, 9, 10, 11, 12, 13 }
Dim hourArray As List(Of Integer)
Select Case CurrentShapeRow(ROW_PERIOD)
Case "ON", "2X16"
hourArray.AddRange(New Integer() {6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21})
Case "2X8", "5X8"
hourArray.AddRange(New Integer() {0, 1, 2, 3, 4, 5, 22, 23})
Case Else
Throw New Exception(String.Format("Unhandled Period: {0}", CurrentShapeRow(ROW_PERIOD)))
End Select
For Each i As Integer In hourArray
Console.WriteLine(i)
Next