Eliminate Duplicate from a Textbox - vb.net

I have Textbox1.Lines (separated by a space)
5 7 10 12 12
7 10 12 13 14
8 10 14 15 19
12 13 14 15 19
21 23 27 29 50
51 53 55 78 80
1 1 7 14 19
2 4 7 8 10
How should I remove duplicates from each line and display something like this:
7 10 12 13 14
8 10 14 15 19
12 13 14 15 19
21 23 27 29 50
51 53 55 78 80
2 4 7 8 10
that is, to remove from the entire line that contains a duplicate number on the same line.
Private Sub BttGamblerInput_Click(sender As Object, e As EventArgs) Handles BttGamblerInput.Click
On Error Resume Next
Dim value As Integer
Dim values As New List(Of String)
For Each strValue As String In TxtGamblerImput.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
If Integer.TryParse(strValue.Trim, value) Then
values.Add(value)
End If
Not worked.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each line In TxtGamblerOutput.Text
Dim originalArray = TxtGamblerOutput.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
Dim checkArray = originalArray.Distinct().ToArray() ' I haven't used this, so check syntax etc when you code it.
If originalArray.Length = checkArray.Length Then
TxtGamblerOutput.Text = checkArray
Else ' <-- perhaps a bonus here?
' Do something with the invalid line
End If
Next line
End Sub
Code 3:
Dim numbers() As Integer = {1, 3, 5, 7, 2, 1, 5, 4, 4, 8}
' get the duplicates
Dim dups As New Dictionary(Of Integer, Integer) ' number, count of number
For i = 0 To numbers.Length - 1
If dups.ContainsKey(numbers(i)) Then
dups(numbers(i)) += 1
Else
dups.Add(numbers(i), 1)
End If
Next
' display them
For Each dup In dups
If dup.Value > 1 Then
Console.WriteLine(dup.Key)
End If
Next
Console.ReadLine()

I haven't coded or tested anything, but you can use the Distinct method from List(of T) and compare the length of the output to the current List. Another approach is to use an ArrayList (c.f. https://stackoverflow.com/a/13428210/9101981 for the bones of getting unique elements).
Pseudo-ish Code using an Array:
for each line in the text input
originalArray = line.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
checkArray = originalArray.Distinct().ToArray() ' I haven't used this, so check syntax etc when you code it.
If originalArray.Length = checkArray.Length then
' Your code here to display the valid line
Else ' <-- perhaps a bonus here?
' Do something with the invalid line
End If
Next line
Some additional reading:
https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.8
and, in particular, note the recommendation to use List.
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.8
noting that this is the Linq Distinct documentation

Related

Convert multiline string into a single line

I would like to do the following thing but I can't - Textbox1.Text
line 1 = 1 2
line 2 = 3 4
line 3 = 1 7
line 4 = 4 9
I want to return this to look like this: yes, and with doubles like 1 1 and so on
Textbox1.Text >Expected Output
line 1 = 1
line 2 = 2
line 3 = 3
line 4 = 4
line 5 = 1
line 6 = 7
line 7 = 4
line 8 = 9
each digit must be on a separate line. that is, on each line that digit, there are now 2 digits on a line.
Code:
source = source.Replace(vbLf, "").Replace(vbCr, "")
You can split original text into values and then combine them together into new lines of text
textbox.Lines = textbox.Lines.
SelectMany(Function(ln) ln.Split(" "c, StringSplitOptions.RemoveEmptyEntries).
ToArray()
each digit must be on a separate line. that is, on each line that
digit, there are now 2 digits on a line.
I'm assuming you don't actually have line x = in your TextBox.
Rather you have:
1 2
3 4
1 7
4 9
and want:
1
2
3
4
1
7
4
9
If so, here's a one-liner that can do it for you...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Lines = String.Join(" ", TextBox1.Lines).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
End Sub

Reverse Number in VB.Net

I want Reverse Number: example:
Textbox1.Text = 2 14 21 22 34 44
a deployment algorithm to do this. make
Expected Output: All Combination possible Reverse.
2 41 21 22 34 44
2 14 12 22 34 44
2 14 21 22 43 44
2 14 21 22 34 44
2 41 12 22 34 44
2 41 12 22 43 44
and so on...
2 14 21 22 34 44
What I try: it works, but it does not carry all the possible combinations, as in the above model.
Dim r As Integer
Public Function Reverse(rn As Integer)
Dim value As Integer
Dim values As New List(Of String)
For Each strValue As String In TextBox1.Text.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
If Integer.TryParse(strValue.Trim, value) Then
values.Add(value)
End If
Next
Dim numbers = Val(TextBox1.Text)
Dim result As Integer
While numbers > 0
rn = numbers Mod 10
result = result * 10 + rn
numbers = numbers \ 10
End While
Reverse = result
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox2.Text = Reverse(r & " ")
End Sub

copy sets of numbers into a listbox from a textbox

I have a textbox that the user puts in sets of numbers(e.g. 32 45 98 56 52 1 23) and I need to copy these numbers into a listbox so that each number is its own item. So far I have this
For Each ch As Char In TextBox20.Text
If Char.IsDigit(ch) Then
ListBox1.Items.Add(ch)
End If
Next
but the problem is that it will copy each digit as an item so we will end up with
3
2
4
5
9
8
5
6
I need it to copy them like this
32
45
98
56
here is a sample of how to do that
Sub addToListBox()
Dim sample As String
Dim v As Variant
Dim i As Integer
sample = "32 45 98 56 52 1 23"
v = Split(sample, " ")
For i = 0 To UBound(v)
If IsNumeric(v(i)) Then
ListBox1.Items.Add(v(i))
End If
Next i
End Sub

Multiple select cases in VB.NET

I have tried the below:
Select Case Combo1.SelectedItem Or Combo2.SelectedItem
But I get the error:
Conversion from String "string here" to type 'Long' is not valid
Is it possible to have multiple select cases?
You separate multiple values by using a comma:
Case Combo1.SelectedItem, Combo2.SelectedItem
Using Or would make it an expression that would be evaluated before compared to the value in the Select.
If your value in the Select is a Long value, then you may need to convert the strings from the controls:
Case CLng(Combo1.SelectedItem), CLng(Combo2.SelectedItem)
To address the question directly, using multiple values as a test expression in a select is not possible:
Select Case v1, v2 'Not possible
Hi Googled and saw this question without an answer. Upon further research, I found this to work for my purposes.
Basically, you start with:
Select case True
Then each case statement can be combinations of the two variables. When both are met, the case is true and will execute the appropriate code.
https://forums.asp.net/t/611892.aspx?To+do+a+select+case+using+two+variables+or+parameters
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim i, j As Integer
Dim value As Integer
For i = 1 To 3
For j = 1 To 5
value = (GetCode(i, j))
TextBox1.Text = TextBox1.Text & "i=" & i & "->j=" & j & "=" & value & vbCrLf
Next
Next
End Sub
Function GetCode(ByVal v1 As Integer, ByVal v2 As Integer) As Integer
Dim retval As Integer
Dim forselect As String
forselect = v1 & v2
Select Case forselect
Case 11
retval = 11
Case 12
retval = 12
Case 13
retval = 13
Case 14
retval = 14
Case 15
retval = 15
Case 21
retval = 21
Case 22
retval = 22
Case 23
retval = 23
Case 24
retval = 24
Case 25
retval = 25
Case 31
retval = 31
Case 32
retval = 32
Case 3, 3
retval = 33
Case 34
retval = 34
Case 35
retval = 35
End Select
Return retval
End Function

how to tokenize chart data for csv file in classic asp

Hi below is the data sample for chart data which I read from URL
ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8
Now in this I want to save this data into a csv file as below
Time A B c
25 3 1 8
75 8 9 7
100 6 7 3
125 7 8 9
150 5 4 56
175 3 7 9
200 2 2 111
225 7 5 8
Actually I have to first read the data into an array in which each element will contain one line of the data file, Now I will have to split the each element by space and store it in a two dimensional array, now for each ith index of each array I have to create a coma separated line then put "\n" at the end of line , and at last save this to a csv file . I don't know much about the syntax of vb-script and classic asp that's why I am facing this problem . Please help me
To get you started:
Sub doReOrder(sFSpecI, sFSpecO, sCol1)
Dim oTS : Set oTS = goFS.OpenTextFile(sFSpecI)
Dim sData : sData = oTS.ReadLine() ' ARRAY ' ' 3 8
Dim aParts : aParts = Split(sData, " ")
Dim nCols : nCols = CByte(aParts(3)) ' Count vs UBound vs Data!
Dim nRows : nRows = CByte(aParts(4))
Dim nRows2 : nRows2 = nRows + 1
oTS.SkipLine ' Y
' get table in one string, prepend col1 name, clean '
sData = "'" & sCol1 & "' " & Replace(oTS.ReadAll(), vbCrLf, " ")
sData = Trim(Replace(sData, "'", """"))
' get table in flat array
aParts = Split(sData, " ")
oTS.Close
Set oTS = goFS.CreateTextFile(sFSpecO, True)
' WScript.Echo Join(aParts, "|")
ReDim aData(nCols) ' hold one (new) row to prep for Join
Dim nRow
For nRow = 0 To nRows
Dim nCol
For nCol = 0 To nCols
' magic column hopping
aData(nCol) = aParts(nRow + nCol * nRows2)
Next
oTS.WriteLine Join(aData, ",")
Next
oTS.Close
End Sub
Test:
Dim sFSpecI : sFSpecI = "..\Data\f1.txt"
Dim sFSpecO : sFSpecO = "..\Data\f1.csv"
Dim sCol1 : sCol1 = "Time" ' dangerous - possibly reserved in SQL
WScript.Echo goFS.OpenTextFile(sFSpecI).ReadAll()
doReOrder sFSpecI, sFSpecO, sCol1
WScript.Echo goFS.OpenTextFile(sFSpecO).ReadAll()
Output:
ARRAY ' ' 3 8
Y
25 75 100 125 150 175 200 225
'A' 3 8 6 7 5 3 2 7
'B' 1 9 7 8 4 7 2 5
'C' 8 7 3 6 56 9 111 8
"Time","A","B","C"
25,3,1,8
75,8,9,7
100,6,7,3
125,7,8,6
150,5,4,56
175,3,7,9
200,2,2,111
225,7,5,8