String to double array - vb.net

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/

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

Assign an integer variable to an integer variable and change name dynamic

Dim LastNumber as Integer = 1
Dim num_0() as Integer = {1, 2, 3, 4, 5}
Dim num_1() as Integer = {6, 7, 8, 9, 10}
Dim num_2() as Integer = {20, 21, 14, 36, 0}
Dim y() As Integer
y(0) = num_0(2)
When I use this code it executes perfectly
But the problem is I want to change the "0" in num_0(2)
When I do...
y(0) = num_ & LastNumber & (2)
This doesnt work
Or
y(0) = ("num_" & LastNumber & "(2)")
This gives me an error that converting a string to an integer is not possible
My question is How can I replace the "0" in num_0(2) with the LastNumber integer variable... so it reads the "8" out of the array num_1(2)
You can use multidimensional array (AKA rectangular array):
Dim num As Integer(,) = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {20, 21, 14, 36, 0} }
y(0) = num(LastNumber, 2)
or jagged array (array of arrays) :
Dim num As Integer()() = { ({1, 2, 3, 4, 5}), ({6, 7, 8, 9, 10}), ({20, 21, 14, 36, 0}) }
y(0) = num(LastNumber)(2)

Rank numbers in a loop 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

how to do vb.net replace on a byte array

I have a byte array of data. How can I find and replacement a part of the byte array with replacement data?
dim foo as byte
foo = MY DATA
If foo was a string, then I would do:
foo = replace( foo, target, replacement )
But foo is a byte array. How can I replace?
Just for the fun of it, here is an implementation:
<TestMethod()>
Public Sub Test_ArrayReplace()
AssertEqualArray({1, 4}, ArrayReplace({1, 2, 3}, {2, 3}, {4}))
AssertEqualArray({1, 4, 5}, ArrayReplace({1, 2, 3, 5}, {2, 3}, {4}))
AssertEqualArray({1, 4, 2}, ArrayReplace({1, 2, 3, 2}, {2, 3}, {4}))
AssertEqualArray({1, 4, 5, 2}, ArrayReplace({1, 2, 3, 2}, {2, 3}, {4, 5}))
AssertEqualArray({1, 2, 3, 8, 9}, ArrayReplace({1, 2, 3, 8, 2, 3, 4}, {2, 3, 4}, {9}))
AssertEqualArray({1, 68, 69, 70, 255}, ArrayReplace({1, 65, 66, 67, 255}, "ABC", "DEF", Encoding.ASCII))
End Sub
Private Sub AssertEqualArray(expected() As Byte, actual() As Byte)
Assert.IsNotNull(expected, "expected")
Assert.IsNotNull(actual, "actual")
Assert.AreEqual(expected.Length, actual.Length, "length")
For index = 0 To actual.Length - 1
Assert.AreEqual(expected(index), actual(index), String.Format("index: {0}", index))
Next
End Sub
Public Function ArrayReplace(data() As Byte, find As String, replacement As String, enc As Encoding) As Byte()
Return ArrayReplace(data, enc.GetBytes(find), enc.GetBytes(replacement))
End Function
Public Function ArrayReplace(data() As Byte, find() As Byte, replacement() As Byte) As Byte()
Dim matchStart As Integer = -1
Dim matchLength As Integer = 0
Using mem = New IO.MemoryStream
For index = 0 To data.Length - 1
If data(index) = find(matchLength) Then
If matchLength = 0 Then matchStart = index
matchLength += 1
If matchLength = find.Length Then
mem.Write(replacement, 0, replacement.Length)
matchLength = 0
End If
Else
If matchLength > 0 Then
mem.Write(data, matchStart, matchLength)
matchLength = 0
End If
mem.WriteByte(data(index))
End If
Next
If matchLength > 0 Then
mem.Write(data, data.Length - matchLength, matchLength)
End If
Dim retVal(mem.Length - 1) As Byte
mem.Position = 0
mem.Read(retVal, 0, retVal.Length)
Return retVal
End Using
End Function
The implementation is surely not perfect and might even be buggy.
Keep in mind that in order to convert a String into an ByteArray you have to know the Encoding of the String.
You can call it this way:
foo = ArrayReplace(foo, "find", "replace", Encoding.UTF8)

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